Skip to content

Commit

Permalink
Fake player singleton, and use consistent UUID for fake player
Browse files Browse the repository at this point in the history
Closes #100
  • Loading branch information
fuj1n committed Jun 6, 2020
1 parent 962394a commit 40763ba
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 10 deletions.
4 changes: 4 additions & 0 deletions src/main/java/slimeknights/tmechworks/TMechworks.java
@@ -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
Expand Up @@ -479,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
@@ -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());
}
}
}
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);
}
}
9 changes: 3 additions & 6 deletions src/main/java/slimeknights/tmechworks/library/Util.java
@@ -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 40763ba

Please sign in to comment.