Skip to content

Commit cda1c7d

Browse files
committed
Fix balloons not firing game events for placing or removal (#8105) and make it so that balloons can be spawned via dispensers
1 parent baaef5a commit cda1c7d

File tree

3 files changed

+36
-3
lines changed

3 files changed

+36
-3
lines changed

src/additions/java/mekanism/additions/common/MekanismAdditions.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import mekanism.additions.client.AdditionsClient;
44
import mekanism.additions.common.block.BlockObsidianTNT;
55
import mekanism.additions.common.config.MekanismAdditionsConfig;
6+
import mekanism.additions.common.entity.EntityBalloon;
7+
import mekanism.additions.common.item.ItemBalloon;
68
import mekanism.additions.common.registries.AdditionsBiomeModifierSerializers;
79
import mekanism.additions.common.registries.AdditionsBlocks;
810
import mekanism.additions.common.registries.AdditionsCreativeTabs;
@@ -16,13 +18,17 @@
1618
import mekanism.common.base.IModModule;
1719
import mekanism.common.config.MekanismModConfig;
1820
import mekanism.common.lib.Version;
21+
import mekanism.common.registration.impl.ItemRegistryObject;
1922
import net.minecraft.core.BlockPos;
23+
import net.minecraft.core.Position;
2024
import net.minecraft.core.dispenser.BlockSource;
2125
import net.minecraft.core.dispenser.DefaultDispenseItemBehavior;
2226
import net.minecraft.resources.ResourceLocation;
27+
import net.minecraft.server.level.ServerLevel;
2328
import net.minecraft.world.item.ItemStack;
2429
import net.minecraft.world.level.block.DispenserBlock;
2530
import net.minecraft.world.level.gameevent.GameEvent;
31+
import net.minecraft.world.phys.Vec3;
2632
import net.neoforged.bus.api.IEventBus;
2733
import net.neoforged.fml.ModContainer;
2834
import net.neoforged.fml.common.Mod;
@@ -113,6 +119,26 @@ protected ItemStack execute(@NotNull BlockSource source, @NotNull ItemStack stac
113119
return super.execute(source, stack);
114120
}
115121
});
122+
DefaultDispenseItemBehavior balloonBehavior = new DefaultDispenseItemBehavior() {
123+
@NotNull
124+
@Override
125+
protected ItemStack execute(@NotNull BlockSource source, @NotNull ItemStack stack) {
126+
ServerLevel level = source.level();
127+
Position position = DispenserBlock.getDispensePosition(source, 1, new Vec3(-0.5, -3.5, -0.5));
128+
EntityBalloon balloon = EntityBalloon.create(level, position.x(), position.y(), position.z(), ((ItemBalloon) stack.getItem()).getColor());
129+
if (balloon == null) {
130+
//Otherwise, if something went very wrong, eject it as a normal item
131+
return super.execute(source, stack);
132+
}
133+
stack.shrink(1);
134+
level.addFreshEntity(balloon);
135+
level.gameEvent(null, GameEvent.ENTITY_PLACE, new Vec3(position.x(), position.y(), position.z()));
136+
return stack;
137+
}
138+
};
139+
for (ItemRegistryObject<ItemBalloon> balloon : AdditionsItems.BALLOONS.values()) {
140+
DispenserBlock.registerBehavior(balloon, balloonBehavior);
141+
}
116142
});
117143
Mekanism.logger.info("Loaded 'Mekanism: Additions' module.");
118144
}

src/additions/java/mekanism/additions/common/entity/EntityBalloon.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import net.minecraft.world.item.ItemStack;
3232
import net.minecraft.world.level.Level;
3333
import net.minecraft.world.level.block.state.BlockState;
34+
import net.minecraft.world.level.gameevent.GameEvent;
3435
import net.minecraft.world.phys.AABB;
3536
import net.minecraft.world.phys.HitResult;
3637
import net.minecraft.world.phys.Vec3;
@@ -247,6 +248,7 @@ private void pop() {
247248
center.z() + 0.6 * random.nextFloat() - 0.3, 1, 0, 0, 0, 0);
248249
}
249250
}
251+
level().gameEvent(GameEvent.ENTITY_DAMAGE, position(), GameEvent.Context.of(this));
250252
discard();
251253
}
252254

src/additions/java/mekanism/additions/common/item/ItemBalloon.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import net.minecraft.world.level.Level;
2424
import net.minecraft.world.level.block.Block;
2525
import net.minecraft.world.level.block.DispenserBlock;
26+
import net.minecraft.world.level.gameevent.GameEvent;
2627
import net.minecraft.world.phys.AABB;
2728
import net.minecraft.world.phys.Vec3;
2829
import org.jetbrains.annotations.NotNull;
@@ -53,6 +54,7 @@ public InteractionResultHolder<ItemStack> use(Level world, @NotNull Player playe
5354
return InteractionResultHolder.fail(stack);
5455
}
5556
world.addFreshEntity(balloon);
57+
world.gameEvent(player, GameEvent.ENTITY_PLACE, pos);
5658
}
5759
if (!player.isCreative()) {
5860
stack.shrink(1);
@@ -102,6 +104,7 @@ public InteractionResult useOn(UseOnContext context) {
102104
}
103105
world.addFreshEntity(balloon);
104106
stack.shrink(1);
107+
world.gameEvent(player, GameEvent.ENTITY_PLACE, pos);
105108
}
106109
return InteractionResult.SUCCESS;
107110
}
@@ -114,10 +117,11 @@ public InteractionResult useOn(UseOnContext context) {
114117
@Override
115118
public InteractionResult interactLivingEntity(@NotNull ItemStack stack, Player player, @NotNull LivingEntity entity, @NotNull InteractionHand hand) {
116119
if (player.isShiftKeyDown()) {
117-
if (!player.level().isClientSide) {
120+
Level level = player.level();
121+
if (!level.isClientSide) {
118122
AABB bound = new AABB(entity.getX() - 0.2, entity.getY() - 0.5, entity.getZ() - 0.2,
119123
entity.getX() + 0.2, entity.getY() + entity.getBbHeight() + 4, entity.getZ() + 0.2);
120-
List<EntityBalloon> balloonsNear = player.level().getEntitiesOfClass(EntityBalloon.class, bound);
124+
List<EntityBalloon> balloonsNear = level.getEntitiesOfClass(EntityBalloon.class, bound);
121125
for (EntityBalloon balloon : balloonsNear) {
122126
if (balloon.latchedEntity == entity) {
123127
return InteractionResult.SUCCESS;
@@ -127,8 +131,9 @@ public InteractionResult interactLivingEntity(@NotNull ItemStack stack, Player p
127131
if (balloon == null) {
128132
return InteractionResult.FAIL;
129133
}
130-
player.level().addFreshEntity(balloon);
134+
level.addFreshEntity(balloon);
131135
stack.shrink(1);
136+
level.gameEvent(player, GameEvent.ENTITY_PLACE, balloon.position());
132137
}
133138
return InteractionResult.SUCCESS;
134139
}

0 commit comments

Comments
 (0)