|
| 1 | +/* |
| 2 | + * Copyright (C) 2024 BlueLib Contributors |
| 3 | + * |
| 4 | + * This Source Code Form is subject to the terms of the MIT License. |
| 5 | + * If a copy of the MIT License was not distributed with this file, |
| 6 | + * You can obtain one at https://opensource.org/licenses/MIT. |
| 7 | + */ |
| 8 | +package software.bluelib.api.molang.context.entity; |
| 9 | + |
| 10 | +import java.util.function.Supplier; |
| 11 | +import net.minecraft.nbt.CompoundTag; |
| 12 | +import net.minecraft.world.entity.Entity; |
| 13 | +import net.minecraft.world.entity.Leashable; |
| 14 | +import org.jetbrains.annotations.NotNull; |
| 15 | +import software.bluelib.api.molang.context.BaseMoLangContext; |
| 16 | + |
| 17 | +public class LeashableMoLang extends BaseMoLangContext { |
| 18 | + |
| 19 | + public LeashableMoLang(@NotNull Supplier<Leashable> pLeashableSup) { |
| 20 | + Leashable leashableEntity = pLeashableSup.get(); |
| 21 | + setVariable("leash_tag", Leashable.LEASH_TAG); |
| 22 | + setVariable("leash_too_far_dist", Leashable.LEASH_TOO_FAR_DIST); |
| 23 | + setVariable("leash_elastic_dist", Leashable.LEASH_ELASTIC_DIST); |
| 24 | + setVariable("get_leash_data", leashableEntity.getLeashData()); |
| 25 | + setVariable("is_leashed", leashableEntity.isLeashed()); |
| 26 | + setVariable("can_have_a_leash_attached_to_it", leashableEntity.canHaveALeashAttachedToIt()); |
| 27 | + setVariable("can_be_leashed", leashableEntity.canBeLeashed()); |
| 28 | + setVariable("get_leash_holder", leashableEntity.getLeashHolder()); |
| 29 | + |
| 30 | + registerFunction("set_leash_data", (args, runtime) -> { |
| 31 | + if (args.size() != 1 || !(args.getFirst() instanceof Leashable.LeashData data)) { |
| 32 | + return leashableEntity.getLeashData(); |
| 33 | + } |
| 34 | + leashableEntity.setLeashData(data); |
| 35 | + return leashableEntity.getLeashData(); |
| 36 | + }); |
| 37 | + |
| 38 | + registerFunction("set_delayed_leash_holder_id", (args, runtime) -> { |
| 39 | + if (args.size() != 1 || !(args.getFirst() instanceof Integer data)) { |
| 40 | + return false; |
| 41 | + } |
| 42 | + leashableEntity.setDelayedLeashHolderId(data); |
| 43 | + return true; |
| 44 | + }); |
| 45 | + |
| 46 | + registerFunction("read_leash_data", (args, runtime) -> { |
| 47 | + if (args.size() != 1 || !(args.getFirst() instanceof CompoundTag data)) { |
| 48 | + return false; |
| 49 | + } |
| 50 | + return leashableEntity.readLeashData(data); |
| 51 | + }); |
| 52 | + |
| 53 | + registerFunction("write_leash_data", (args, runtime) -> { |
| 54 | + if (args.size() != 2 || (!(args.getFirst() instanceof CompoundTag) && !(args.get(1) instanceof Leashable.LeashData))) { |
| 55 | + return false; |
| 56 | + } |
| 57 | + CompoundTag data = (CompoundTag) args.getFirst(); |
| 58 | + Leashable.LeashData leashData = (Leashable.LeashData) args.get(1); |
| 59 | + leashableEntity.writeLeashData(data, leashData); |
| 60 | + return leashableEntity.readLeashData(data); |
| 61 | + }); |
| 62 | + |
| 63 | + registerFunction("drop_leash", (args, runtime) -> { |
| 64 | + if (args.size() != 2 || (!(args.getFirst() instanceof Boolean) && !(args.get(1) instanceof Boolean))) { |
| 65 | + return false; |
| 66 | + } |
| 67 | + boolean sendPacket = (Boolean) args.getFirst(); |
| 68 | + boolean dropItem = (Boolean) args.get(1); |
| 69 | + leashableEntity.dropLeash(sendPacket, dropItem); |
| 70 | + return true; |
| 71 | + }); |
| 72 | + |
| 73 | + registerFunction("handle_leash_at_distance", (args, runtime) -> { |
| 74 | + if (args.size() != 2 || (!(args.getFirst() instanceof Entity) && !(args.get(1) instanceof Number))) { |
| 75 | + return false; |
| 76 | + } |
| 77 | + Entity entity = (Entity) args.getFirst(); |
| 78 | + float distance = ((Number) args.get(1)).floatValue(); |
| 79 | + leashableEntity.handleLeashAtDistance(entity, distance); |
| 80 | + return true; |
| 81 | + }); |
| 82 | + |
| 83 | + registerFunction("leash_too_far_behaviour", (args, runtime) -> { |
| 84 | + leashableEntity.leashTooFarBehaviour(); |
| 85 | + return true; |
| 86 | + }); |
| 87 | + |
| 88 | + registerFunction("close_range_leash_behaviour", (args, runtime) -> { |
| 89 | + if (args.size() != 1 || !(args.getFirst() instanceof Entity entity)) { |
| 90 | + return false; |
| 91 | + } |
| 92 | + leashableEntity.closeRangeLeashBehaviour(entity); |
| 93 | + return true; |
| 94 | + }); |
| 95 | + |
| 96 | + registerFunction("elastic_range_leash_behaviour", (args, runtime) -> { |
| 97 | + if (args.size() != 2 || (!(args.getFirst() instanceof Entity) && !(args.get(1) instanceof Number))) { |
| 98 | + return false; |
| 99 | + } |
| 100 | + Entity entity = (Entity) args.getFirst(); |
| 101 | + float distance = ((Number) args.get(1)).floatValue(); |
| 102 | + leashableEntity.elasticRangeLeashBehaviour(entity, distance); |
| 103 | + return true; |
| 104 | + }); |
| 105 | + |
| 106 | + registerFunction("set_leash_to", (args, runtime) -> { |
| 107 | + if (args.size() != 2 || (!(args.getFirst() instanceof Entity) && !(args.get(1) instanceof Boolean))) { |
| 108 | + return false; |
| 109 | + } |
| 110 | + Entity entity = (Entity) args.getFirst(); |
| 111 | + boolean broadcast = (Boolean) args.get(1); |
| 112 | + leashableEntity.setLeashedTo(entity, broadcast); |
| 113 | + return true; |
| 114 | + }); |
| 115 | + } |
| 116 | +} |
0 commit comments