Use custom FakePlayer for converting Zombie Villagers

This commit is contained in:
reidbhuntley 2021-07-12 00:43:23 -04:00
parent b70608b030
commit cb41ac679d
7 changed files with 28 additions and 21 deletions

View file

@ -27,8 +27,8 @@ public class ContraptionHandler {
static WorldAttached<List<AbstractContraptionEntity>> queuedAdditions; static WorldAttached<List<AbstractContraptionEntity>> queuedAdditions;
static { static {
loadedContraptions = new WorldAttached<>(HashMap::new); loadedContraptions = new WorldAttached<>($ -> new HashMap<>());
queuedAdditions = new WorldAttached<>(() -> ObjectLists.synchronize(new ObjectArrayList<>())); queuedAdditions = new WorldAttached<>($ -> ObjectLists.synchronize(new ObjectArrayList<>()));
} }
public static void tick(World world) { public static void tick(World world) {

View file

@ -80,10 +80,10 @@ public class CapabilityMinecartController implements ICapabilitySerializable<Com
} }
static { static {
loadedMinecartsByUUID = new WorldAttached<>(HashMap::new); loadedMinecartsByUUID = new WorldAttached<>($ -> new HashMap<>());
loadedMinecartsWithCoupling = new WorldAttached<>(HashSet::new); loadedMinecartsWithCoupling = new WorldAttached<>($ -> new HashSet<>());
queuedAdditions = new WorldAttached<>(() -> ObjectLists.synchronize(new ObjectArrayList<>())); queuedAdditions = new WorldAttached<>($ -> ObjectLists.synchronize(new ObjectArrayList<>()));
queuedUnloads = new WorldAttached<>(() -> ObjectLists.synchronize(new ObjectArrayList<>())); queuedUnloads = new WorldAttached<>($ -> ObjectLists.synchronize(new ObjectArrayList<>()));
} }
public static void tick(World world) { public static void tick(World world) {

View file

@ -272,7 +272,7 @@ public abstract class FluidTransportBehaviour extends TileEntityBehaviour {
// for switching TEs, but retaining flows // for switching TEs, but retaining flows
public static final WorldAttached<Map<BlockPos, Map<Direction, PipeConnection>>> interfaceTransfer = public static final WorldAttached<Map<BlockPos, Map<Direction, PipeConnection>>> interfaceTransfer =
new WorldAttached<>(HashMap::new); new WorldAttached<>($ -> new HashMap<>());
public static void cacheFlows(IWorld world, BlockPos pos) { public static void cacheFlows(IWorld world, BlockPos pos) {
FluidTransportBehaviour pipe = TileEntityBehaviour.get(world, pos, FluidTransportBehaviour.TYPE); FluidTransportBehaviour pipe = TileEntityBehaviour.get(world, pos, FluidTransportBehaviour.TYPE);

View file

@ -3,12 +3,15 @@ package com.simibubi.create.content.curiosities.weapons;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.Optional; import java.util.Optional;
import java.util.UUID;
import java.util.function.BiPredicate; import java.util.function.BiPredicate;
import java.util.function.Predicate; import java.util.function.Predicate;
import com.mojang.authlib.GameProfile;
import com.mojang.datafixers.util.Pair; import com.mojang.datafixers.util.Pair;
import com.simibubi.create.AllItems; import com.simibubi.create.AllItems;
import com.simibubi.create.Create; import com.simibubi.create.Create;
import com.simibubi.create.foundation.utility.WorldAttached;
import net.minecraft.block.Block; import net.minecraft.block.Block;
import net.minecraft.block.BlockState; import net.minecraft.block.BlockState;
@ -18,7 +21,6 @@ import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.item.FallingBlockEntity; import net.minecraft.entity.item.FallingBlockEntity;
import net.minecraft.entity.monster.ZombieVillagerEntity; import net.minecraft.entity.monster.ZombieVillagerEntity;
import net.minecraft.entity.passive.FoxEntity; import net.minecraft.entity.passive.FoxEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.Food; import net.minecraft.item.Food;
import net.minecraft.item.Foods; import net.minecraft.item.Foods;
import net.minecraft.item.Item; import net.minecraft.item.Item;
@ -43,11 +45,16 @@ import net.minecraft.world.IWorld;
import net.minecraft.world.World; import net.minecraft.world.World;
import net.minecraft.world.server.ServerWorld; import net.minecraft.world.server.ServerWorld;
import net.minecraftforge.common.IPlantable; import net.minecraftforge.common.IPlantable;
import net.minecraftforge.common.util.FakePlayerFactory; import net.minecraftforge.common.util.FakePlayer;
import net.minecraftforge.registries.IRegistryDelegate; import net.minecraftforge.registries.IRegistryDelegate;
public class PotatoCannonProjectileTypes { public class PotatoCannonProjectileTypes {
private static final GameProfile ZOMBIE_CONVERTER_NAME =
new GameProfile(UUID.fromString("be12d3dc-27d3-4992-8c97-66be53fd49c5"), "Converter");
private static final WorldAttached<FakePlayer> ZOMBIE_CONVERTERS =
new WorldAttached<>(w -> new FakePlayer((ServerWorld) w, ZOMBIE_CONVERTER_NAME));
public static final Map<ResourceLocation, PotatoCannonProjectileTypes> ALL = new HashMap<>(); public static final Map<ResourceLocation, PotatoCannonProjectileTypes> ALL = new HashMap<>();
public static final Map<IRegistryDelegate<Item>, PotatoCannonProjectileTypes> ITEM_MAP = new HashMap<>(); public static final Map<IRegistryDelegate<Item>, PotatoCannonProjectileTypes> ITEM_MAP = new HashMap<>();
public static final PotatoCannonProjectileTypes public static final PotatoCannonProjectileTypes
@ -155,8 +162,8 @@ public class PotatoCannonProjectileTypes {
if (world.isRemote) if (world.isRemote)
return false; return false;
PlayerEntity dummy = FakePlayerFactory.getMinecraft((ServerWorld) world); FakePlayer dummy = ZOMBIE_CONVERTERS.get(world);
dummy.setHeldItem(Hand.MAIN_HAND, new ItemStack(Items.GOLDEN_APPLE)); dummy.setHeldItem(Hand.MAIN_HAND, new ItemStack(Items.GOLDEN_APPLE, 1));
((ZombieVillagerEntity) entity).interactMob(dummy, Hand.MAIN_HAND); ((ZombieVillagerEntity) entity).interactMob(dummy, Hand.MAIN_HAND);
return true; return true;
}) })

View file

@ -24,7 +24,7 @@ import net.minecraft.world.IWorld;
public class LinkedControllerServerHandler { public class LinkedControllerServerHandler {
public static WorldAttached<Map<UUID, Collection<ManualFrequencyEntry>>> receivedInputs = public static WorldAttached<Map<UUID, Collection<ManualFrequencyEntry>>> receivedInputs =
new WorldAttached<>(HashMap::new); new WorldAttached<>($ -> new HashMap<>());
static final int TIMEOUT = 30; static final int TIMEOUT = 30;
public static void tick(IWorld world) { public static void tick(IWorld world) {

View file

@ -23,7 +23,7 @@ public class SchematicInstances {
public static WorldAttached<Cache<Integer, SchematicWorld>> loadedSchematics; public static WorldAttached<Cache<Integer, SchematicWorld>> loadedSchematics;
static { static {
loadedSchematics = new WorldAttached<>(() -> CacheBuilder.newBuilder() loadedSchematics = new WorldAttached<>($ -> CacheBuilder.newBuilder()
.expireAfterAccess(5, TimeUnit.MINUTES) .expireAfterAccess(5, TimeUnit.MINUTES)
.build()); .build());
} }

View file

@ -4,7 +4,7 @@ import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.function.Supplier; import java.util.function.Function;
import javax.annotation.Nullable; import javax.annotation.Nullable;
@ -14,9 +14,9 @@ public class WorldAttached<T> {
static List<Map<IWorld, ?>> allMaps = new ArrayList<>(); static List<Map<IWorld, ?>> allMaps = new ArrayList<>();
Map<IWorld, T> attached; Map<IWorld, T> attached;
private Supplier<T> factory; private Function<IWorld, T> factory;
public WorldAttached(Supplier<T> factory) { public WorldAttached(Function<IWorld, T> factory) {
this.factory = factory; this.factory = factory;
attached = new HashMap<>(); attached = new HashMap<>();
allMaps.add(attached); allMaps.add(attached);
@ -31,7 +31,7 @@ public class WorldAttached<T> {
T t = attached.get(world); T t = attached.get(world);
if (t != null) if (t != null)
return t; return t;
T entry = factory.get(); T entry = factory.apply(world);
put(world, entry); put(world, entry);
return entry; return entry;
} }