Skip to content

Commit

Permalink
Merge branch 'release/v2.1.1'
Browse files Browse the repository at this point in the history
  • Loading branch information
fuj1n committed Jun 6, 2020
2 parents 5e84ad5 + e7f6fb1 commit 83a830b
Show file tree
Hide file tree
Showing 7 changed files with 142 additions and 15 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
org.gradle.jvmargs=-Xmx3G
org.gradle.daemon=false

mod_version=2.1.0
mod_version=2.1.1

minecraft_version=1.15.2
minecraft_version_short=1.15
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/slimeknights/tmechworks/TMechworks.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package slimeknights.tmechworks;

import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.eventbus.api.IEventBus;
import net.minecraftforge.fml.DistExecutor;
import net.minecraftforge.fml.common.Mod;
Expand All @@ -15,6 +16,7 @@
import slimeknights.tmechworks.common.CommonProxy;
import slimeknights.tmechworks.common.MechworksContent;
import slimeknights.tmechworks.common.config.MechworksConfig;
import slimeknights.tmechworks.common.entities.MechworksFakePlayer;
import slimeknights.tmechworks.common.network.PacketHandler;

import java.nio.file.Path;
Expand Down Expand Up @@ -43,6 +45,8 @@ public TMechworks() {
bus.addListener(this::postInit);
bus.addListener(this::setupClient);

MinecraftForge.EVENT_BUS.addListener(MechworksFakePlayer::onWorldUnload);

content = new MechworksContent();
bus.register(content);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,23 @@
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.BlockRayTraceResult;
import net.minecraft.util.math.Vec3d;
import net.minecraft.util.text.*;
import net.minecraft.util.text.ITextComponent;
import net.minecraft.util.text.StringTextComponent;
import net.minecraft.util.text.TranslationTextComponent;
import net.minecraft.world.World;
import net.minecraft.world.server.ServerWorld;
import net.minecraft.world.storage.loot.LootContext;
import net.minecraft.world.storage.loot.LootParameters;
import net.minecraftforge.common.ToolType;
import net.minecraftforge.common.util.BlockSnapshot;
import net.minecraftforge.common.util.FakePlayer;
import net.minecraftforge.common.util.LazyOptional;
import net.minecraftforge.event.ForgeEventFactory;
import slimeknights.tmechworks.common.config.MechworksConfig;
import net.minecraftforge.items.wrapper.InvWrapper;
import slimeknights.tmechworks.common.MechworksContent;
import slimeknights.tmechworks.common.blocks.DrawbridgeBlock;
import slimeknights.tmechworks.common.blocks.RedstoneMachineBlock;
import slimeknights.tmechworks.common.config.MechworksConfig;
import slimeknights.tmechworks.common.inventory.DrawbridgeContainer;
import slimeknights.tmechworks.common.inventory.FragmentedInventory;
import slimeknights.tmechworks.common.items.MachineUpgradeItem;
Expand Down Expand Up @@ -62,13 +66,19 @@ public class DrawbridgeTileEntity extends RedstoneMachineTileEntity implements I
private int extendedLength;
private float cooldown;

private static boolean isCapAccess;

private long lastWorldTime;

public DrawbridgeTileEntity() {
super(MechworksContent.TileEntities.drawbridge, new TranslationTextComponent(Util.prefix("inventory.drawbridge")), UPGRADES_SIZE + 1);

upgrades = new FragmentedInventory(this, 0, UPGRADES_SIZE).overrideStackLimit(1).setValidItemsPredicate(stack -> stack.getItem() instanceof MachineUpgradeItem);
slots = new FragmentedInventory(this, UPGRADES_SIZE, 1).setValidItemsPredicate(stack -> stack.getItem() instanceof BlockItem && !DrawbridgeBlock.BLACKLIST.contains(Block.getBlockFromItem(stack.getItem()))).overrideStackLimit(64);

itemHandlerCap.invalidate();
itemHandler = new DrawbridgeItemHandler(this);
itemHandlerCap = LazyOptional.of(() -> itemHandler);
}

@Override
Expand Down Expand Up @@ -469,9 +479,7 @@ public void setPlaceDirectionRelativeToBlock(Direction direction) {
}

public void updateFakePlayer(BlockPos pos) {
if (fakePlayer == null || fakePlayer.get() == null) {
fakePlayer = Util.createFakePlayer(world);
}
fakePlayer = Util.getFakePlayer(world);

if (fakePlayer == null) {
return;
Expand Down Expand Up @@ -537,6 +545,17 @@ public void setInventorySlotContents(int slot, @Nonnull ItemStack itemstack) {
computeStats();
}

@Override
public boolean isItemValidForSlot(int slot, @Nonnull ItemStack stack) {
if(!isCapAccess)
return false;

if(!slots.isSlotInInventory(slot - slots.getStartSlot()))
return false;

return super.isItemValidForSlot(slot, stack) && slots.isItemValidForSlot(slot - slots.getStartSlot(), stack);
}

public FakePlayer getFakePlayer(BlockPos pos) {
updateFakePlayer(pos);

Expand Down Expand Up @@ -598,7 +617,7 @@ public ActionResultType doPlaceBlock(DrawbridgeItemUseContext context) {
if (!(itemstack.getItem() instanceof BucketItem)) // if not bucket
world.captureBlockSnapshots = true;

ItemStack copy = itemstack.isDamageable() ? itemstack.copy() : null;
ItemStack copy = itemstack.copy();
ActionResultType ret = item.tryPlace(context);
if (itemstack.isEmpty())
ForgeEventFactory.onPlayerDestroyItem(player, copy, context.getHand());
Expand Down Expand Up @@ -684,6 +703,59 @@ public BlockPos getPos() {
}
}

private static class DrawbridgeItemHandler extends InvWrapper {
private DrawbridgeTileEntity te;

public DrawbridgeItemHandler(DrawbridgeTileEntity inv) {
super(inv);

te = inv;
}

@Nonnull
@Override
public ItemStack insertItem(int slotAbs, @Nonnull ItemStack stack, boolean simulate) {
int slot = slotAbs - te.slots.getStartSlot();

// Disallow inserting anywhere but in main inventory slots
if(slot < 0 || slot >= te.slots.getSizeInventory())
return stack;

isCapAccess = true;
ItemStack out = super.insertItem(slotAbs, stack, simulate);
isCapAccess = false;
return out;
}

@Nonnull
@Override
public ItemStack extractItem(int slotAbs, int amount, boolean simulate) {
int slot = slotAbs - te.slots.getStartSlot();

// Disallow inserting anywhere but in main inventory slots
if(slot < 0 || slot >= te.slots.getSizeInventory())
return ItemStack.EMPTY;

isCapAccess = true;
ItemStack out = super.extractItem(slotAbs, amount, simulate);
isCapAccess = false;
return out;
}

@Override
public boolean isItemValid(int slot, @Nonnull ItemStack stack) {
isCapAccess = true;
boolean out = super.isItemValid(slot, stack);
isCapAccess = false;
return out;
}

@Override
public int getSlotLimit(int slot) {
return te.slots.getInventoryStackLimit();
}
}

private static class DrawbridgeTools {
public static final ItemStack PICKAXE;
public static final ItemStack AXE;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package slimeknights.tmechworks.common.entities;

import com.mojang.authlib.GameProfile;
import net.minecraft.potion.EffectInstance;
import net.minecraft.world.IWorld;
import net.minecraft.world.server.ServerWorld;
import net.minecraftforge.common.util.FakePlayer;
import net.minecraftforge.event.world.WorldEvent;
import slimeknights.tmechworks.TMechworks;

import java.lang.ref.WeakReference;
import java.util.UUID;

@SuppressWarnings("EntityConstructor")
public class MechworksFakePlayer extends FakePlayer {
public static final String NAME = "MechworksWorker";
public static final UUID ID = UUID.nameUUIDFromBytes((TMechworks.modId + ".FakePlayer").getBytes());
public static final GameProfile PROFILE = new GameProfile(ID, NAME);

private static MechworksFakePlayer instance;

private MechworksFakePlayer(ServerWorld world, GameProfile name) {
super(world, name);
}

public static WeakReference<FakePlayer> getInstance(ServerWorld world) {
if (instance == null) {
instance = new MechworksFakePlayer(world, PROFILE);
}

instance.world = world;
return new WeakReference<>(instance);
}

private static void releaseInstance(IWorld world) {
// If the fake player has a reference to the world getting unloaded,
// null out the fake player so that the world can unload
if (instance != null && instance.world == world) {
instance = null;
}
}

@Override
public boolean isPotionApplicable(EffectInstance potioneffectIn) {
return false;
}

public static void onWorldUnload(WorldEvent.Unload event) {
if (event.getWorld() instanceof ServerWorld) {
releaseInstance(event.getWorld());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import net.minecraftforge.fml.common.Mod;
import org.apache.commons.lang3.StringUtils;
import slimeknights.tmechworks.TMechworks;
import slimeknights.tmechworks.common.entities.MechworksFakePlayer;
import slimeknights.tmechworks.library.Util;

@Mod.EventBusSubscriber(modid = TMechworks.modId)
Expand All @@ -18,7 +19,7 @@ public class DrawbridgeSoundEventListener {
@SubscribeEvent
public static void onSound(PlaySoundAtEntityEvent event){
Entity entity = event.getEntity();
if(entity instanceof FakePlayer && StringUtils.equals(((FakePlayer) entity).getGameProfile().getName(), Util.FAKEPLAYER_NAME))
if(entity instanceof FakePlayer && StringUtils.equals(((FakePlayer) entity).getGameProfile().getName(), MechworksFakePlayer.NAME))
event.setCanceled(true);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ public void closeInventory(PlayerEntity player) {

@Override
public boolean isItemValidForSlot(int slot, ItemStack itemStack) {
return validItems.test(itemStack) && parent.isItemValidForSlot(getSlot(slot), itemStack);
return isItemValidForValidatingSlot(slot, itemStack);
}

public boolean isSlotInInventory(int i) {
Expand Down
9 changes: 3 additions & 6 deletions src/main/java/slimeknights/tmechworks/library/Util.java
Original file line number Diff line number Diff line change
@@ -1,33 +1,30 @@
package slimeknights.tmechworks.library;

import com.mojang.authlib.GameProfile;
import net.minecraft.util.ResourceLocation;
import net.minecraft.world.World;
import net.minecraft.world.server.ServerWorld;
import net.minecraftforge.common.util.FakePlayer;
import net.minecraftforge.common.util.FakePlayerFactory;
import org.apache.commons.lang3.StringUtils;
import slimeknights.tmechworks.TMechworks;
import slimeknights.tmechworks.common.entities.MechworksFakePlayer;

import java.lang.ref.WeakReference;
import java.util.Locale;
import java.util.Random;
import java.util.UUID;

public class Util
{
public static final String RESOURCE = TMechworks.modId;
public static final Random rand = new Random();
public static final String FAKEPLAYER_NAME = "MechworksWorker";

public static WeakReference<FakePlayer> createFakePlayer (World world)
public static WeakReference<FakePlayer> getFakePlayer(World world)
{
if (!(world instanceof ServerWorld))
{
return null;
}

return new WeakReference<>(FakePlayerFactory.get((ServerWorld) world, new GameProfile(UUID.randomUUID(), FAKEPLAYER_NAME)));
return MechworksFakePlayer.getInstance((ServerWorld)world);
}

/**
Expand Down

0 comments on commit 83a830b

Please sign in to comment.