Skip to content

Commit

Permalink
You can now reset the opened state of minecarts.
Browse files Browse the repository at this point in the history
  • Loading branch information
noobanidus committed Sep 27, 2021
1 parent 6cbed02 commit 726d92d
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,10 @@ public void setOpened() {
this.opened = true;
}

public void setClosed () {
this.opened = false;
}

@Override
public boolean isInvulnerableTo(DamageSource source) {
if (this.isInvulnerable() && source != DamageSource.OUT_OF_WORLD && !source.isCreativePlayer()) {
Expand Down
37 changes: 37 additions & 0 deletions src/main/java/noobanidus/mods/lootr/networking/CloseCart.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package noobanidus.mods.lootr.networking;


import net.minecraft.network.PacketBuffer;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;
import net.minecraftforge.fml.network.NetworkEvent;
import noobanidus.mods.lootr.networking.client.ClientHandlers;

import java.util.function.Supplier;

public class CloseCart {
public int entityId;

public CloseCart(PacketBuffer buffer) {
this.entityId = buffer.readInt();
}

public CloseCart(int entityId) {
this.entityId = entityId;
}

public void encode(PacketBuffer buf) {
buf.writeInt(this.entityId);
}

public void handle(Supplier<NetworkEvent.Context> context) {
context.get().enqueueWork(() -> handle(this, context));
context.get().setPacketHandled(true);
}

@OnlyIn(Dist.CLIENT)
private static void handle(CloseCart message, Supplier<NetworkEvent.Context> context) {
ClientHandlers.handleCloseCart(message, context);
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public class PacketHandler {

public static void registerMessages() {
registerMessage(OpenCart.class, OpenCart::encode, OpenCart::new, OpenCart::handle);
registerMessage(CloseCart.class, CloseCart::encode, CloseCart::new, CloseCart::handle);
}

public static void sendToInternal(Object msg, ServerPlayerEntity player) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import net.minecraftforge.fml.network.NetworkEvent;
import noobanidus.mods.lootr.Lootr;
import noobanidus.mods.lootr.entity.LootrChestMinecartEntity;
import noobanidus.mods.lootr.networking.CloseCart;
import noobanidus.mods.lootr.networking.OpenCart;

import java.util.function.Supplier;
Expand Down Expand Up @@ -33,4 +34,27 @@ public static void handleOpenCart(OpenCart message, Supplier<NetworkEvent.Contex

((LootrChestMinecartEntity) cart).setOpened();
}

public static void handleCloseCart (CloseCart message, Supplier<NetworkEvent.Context> context) {
World world = Minecraft.getInstance().level;
if (world == null) {
Lootr.LOG.info("Unable to mark entity with id '" + message.entityId + "' as closed as world is null.");
context.get().setPacketHandled(true);
return;
}
Entity cart = world.getEntity(message.entityId);
if (cart == null) {
Lootr.LOG.info("Unable to mark entity with id '" + message.entityId + "' as closed as entity is null.");
context.get().setPacketHandled(true);
return;
}

if (!(cart instanceof LootrChestMinecartEntity)) {
Lootr.LOG.info("Unable to mark entity with id '" + message.entityId + "' as closed as entity is not a Lootr minecart.");
context.get().setPacketHandled(true);
return;
}

((LootrChestMinecartEntity) cart).setClosed();
}
}
4 changes: 2 additions & 2 deletions src/main/java/noobanidus/mods/lootr/util/ChestUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import noobanidus.mods.lootr.data.NewChestData;
import noobanidus.mods.lootr.entity.LootrChestMinecartEntity;
import noobanidus.mods.lootr.init.ModStats;
import noobanidus.mods.lootr.networking.CloseCart;
import noobanidus.mods.lootr.networking.OpenCart;
import noobanidus.mods.lootr.networking.PacketHandler;
import noobanidus.mods.lootr.tiles.SpecialLootInventoryTile;
Expand Down Expand Up @@ -62,8 +63,7 @@ public static void handleLootCartSneak(World world, LootrChestMinecartEntity car
}

cart.getOpeners().remove(player.getUUID());
// TODO: CloseCart packet
OpenCart open = new OpenCart(cart.getId());
CloseCart open = new CloseCart(cart.getId());
PacketHandler.sendInternal(PacketDistributor.TRACKING_ENTITY.with(() -> cart), open);
}

Expand Down

0 comments on commit 726d92d

Please sign in to comment.