This commit is contained in:
Marcel Konrad
2018-02-05 17:31:55 +01:00
parent cc0f1e43b6
commit 9ba0331404
167 changed files with 18034 additions and 6 deletions

View File

@@ -0,0 +1,38 @@
package exopandora.worldhandler.helper;
import java.util.List;
import exopandora.worldhandler.builder.impl.BuilderAdvancement.EnumMode;
import net.minecraft.advancements.AdvancementManager;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
@SideOnly(Side.CLIENT)
public class AdvancementHelper
{
public static final AdvancementManager ADVANCEMENT_MANAGER = new AdvancementManager(null);
private final Node modes = new Node();
public AdvancementHelper()
{
this.init();
}
private void init()
{
for(EnumMode mode : EnumMode.values())
{
this.modes.addNode(mode.toString());
}
}
public List<Node> getModes()
{
if(this.modes != null)
{
return this.modes.getEntries();
}
return null;
}
}

View File

@@ -0,0 +1,223 @@
package exopandora.worldhandler.helper;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;
import org.apache.commons.lang3.ArrayUtils;
import exopandora.worldhandler.builder.impl.BuilderSetblock;
import exopandora.worldhandler.builder.types.Coordinate;
import exopandora.worldhandler.config.ConfigSettings;
import exopandora.worldhandler.main.WorldHandler;
import exopandora.worldhandler.util.UtilPlayer;
import io.netty.buffer.Unpooled;
import net.minecraft.block.Block;
import net.minecraft.client.Minecraft;
import net.minecraft.init.Blocks;
import net.minecraft.network.PacketBuffer;
import net.minecraft.network.play.client.CPacketCustomPayload;
import net.minecraft.tileentity.TileEntityCommandBlock;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.RayTraceResult;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
@SideOnly(Side.CLIENT)
public class BlockHelper
{
private static BlockPos POS_1 = BlockPos.ORIGIN;
private static BlockPos POS_2 = BlockPos.ORIGIN;
private static final List<Consumer<BlockPos>> POS_1_OBSERVERS = new ArrayList<Consumer<BlockPos>>();
private static final List<Consumer<BlockPos>> POS_2_OBSERVERS = new ArrayList<Consumer<BlockPos>>();
private static final Block[] BLACKLIST = new Block[] {Blocks.AIR, Blocks.WATER, Blocks.LAVA};
public static BlockPos getFocusedBlockPos()
{
RayTraceResult rayTrace = Minecraft.getMinecraft().objectMouseOver;
if(rayTrace != null)
{
if(rayTrace.typeOfHit.equals(RayTraceResult.Type.BLOCK))
{
BlockPos position = rayTrace.getBlockPos();
if(!ArrayUtils.contains(BLACKLIST, Minecraft.getMinecraft().world.getBlockState(position).getBlock()))
{
return position;
}
}
}
return Minecraft.getMinecraft().player.getPosition();
}
public static boolean isFocusedBlockEqualTo(Block block)
{
return Block.isEqualTo(getFocusedBlock(), block);
}
public static Block getFocusedBlock()
{
return Minecraft.getMinecraft().world.getBlockState(getFocusedBlockPos()).getBlock();
}
public static BlockPos setX(BlockPos pos, double x)
{
return new BlockPos(x, pos.getY(), pos.getZ());
}
public static BlockPos setY(BlockPos pos, double y)
{
return new BlockPos(pos.getX(), y, pos.getZ());
}
public static BlockPos setZ(BlockPos pos, double z)
{
return new BlockPos(pos.getX(), pos.getY(), z);
}
public static BlockPos getPos1()
{
return POS_1;
}
public static void setPos1(BlockPos pos1)
{
if(POS_1 != null && !POS_1.equals(pos1))
{
POS_1 = pos1;
for(Consumer<BlockPos> observer : POS_1_OBSERVERS)
{
observer.accept(POS_1);
}
}
}
public static BlockPos getPos2()
{
return POS_2;
}
public static void setPos2(BlockPos pos2)
{
if(POS_2 != null && !POS_2.equals(pos2))
{
POS_2 = pos2;
for(Consumer<BlockPos> observer : POS_2_OBSERVERS)
{
observer.accept(POS_2);
}
}
}
public static void addPos1Observer(Consumer<BlockPos> observer)
{
POS_1_OBSERVERS.add(observer);
}
public static void removePos1Observer(Consumer<BlockPos> observer)
{
POS_1_OBSERVERS.remove(observer);
}
public static void addPos2Observer(Consumer<BlockPos> observer)
{
POS_2_OBSERVERS.add(observer);
}
public static void removePos2Observer(Consumer<BlockPos> observer)
{
POS_2_OBSERVERS.add(observer);
}
public static boolean setCommandBlockNearPlayer(String command)
{
if(UtilPlayer.canIssueCommand())
{
BlockPos player = new BlockPos(Minecraft.getMinecraft().player.posX, Minecraft.getMinecraft().player.posY, Minecraft.getMinecraft().player.posZ);
BlockPos block = player;
BlockPos button = player;
int meta = 0;
switch(UtilPlayer.getPlayerDirection())
{
case 0:
block = block.add(0, 0, 2);
button = button.add(0, 0, 1);
meta = 4;
break;
case 1:
block = block.add(-2, 0, 0);
button = button.add(-1, 0, 0);
meta = 1;
break;
case 2:
block = block.add(0, 0, -2);
button = button.add(0, 0, -1);
meta = 3;
break;
case 3:
block = block.add(2, 0, 0);
button = button.add(1, 0, 0);
meta = 2;
break;
}
boolean flag = false;
if(Minecraft.getMinecraft().world.isAirBlock(block))
{
Minecraft.getMinecraft().player.sendChatMessage(new BuilderSetblock(block, Blocks.COMMAND_BLOCK.getRegistryName(), 0, ConfigSettings.getMode()).toActualCommand());
Minecraft.getMinecraft().player.sendChatMessage(new BuilderSetblock(button, Blocks.WOODEN_BUTTON.getRegistryName(), meta, ConfigSettings.getMode()).toActualCommand());
flag = true;
}
if(Minecraft.getMinecraft().world.getBlockState(block).getBlock().equals(Blocks.COMMAND_BLOCK) || flag)
{
if(Minecraft.getMinecraft().getConnection() != null)
{
PacketBuffer packetbuffer = new PacketBuffer(Unpooled.buffer());
packetbuffer.writeInt(block.getX());
packetbuffer.writeInt(block.getY());
packetbuffer.writeInt(block.getZ());
packetbuffer.writeString(command);
packetbuffer.writeBoolean(true);
packetbuffer.writeString(TileEntityCommandBlock.Mode.REDSTONE.name());
packetbuffer.writeBoolean(false);
packetbuffer.writeBoolean(false);
Minecraft.getMinecraft().getConnection().sendPacket(new CPacketCustomPayload("MC|AutoCmd", packetbuffer));
return true;
}
}
}
return false;
}
public static void setBlockNearPlayer(Block block, byte southMeta, byte westMeta, byte northMeta, byte eastMeta)
{
int direction = UtilPlayer.getPlayerDirection();
switch(direction)
{
case 0:
WorldHandler.sendCommand(new BuilderSetblock(new Coordinate(), new Coordinate(), new Coordinate(2, true), block.getRegistryName(), southMeta, ConfigSettings.getMode()));
break;
case 1:
WorldHandler.sendCommand(new BuilderSetblock(new Coordinate(-2, true), new Coordinate(), new Coordinate(), block.getRegistryName(), westMeta, ConfigSettings.getMode()));
break;
case 2:
WorldHandler.sendCommand(new BuilderSetblock(new Coordinate(), new Coordinate(), new Coordinate(-2, true), block.getRegistryName(), northMeta, ConfigSettings.getMode()));
break;
case 3:
WorldHandler.sendCommand(new BuilderSetblock(new Coordinate(2, true), new Coordinate(), new Coordinate(), block.getRegistryName(), eastMeta, ConfigSettings.getMode()));
break;
}
}
}

View File

@@ -0,0 +1,80 @@
package exopandora.worldhandler.helper;
import java.util.Map;
import java.util.Map.Entry;
import java.util.stream.Collectors;
import javax.annotation.Nullable;
import net.minecraft.entity.Entity;
import net.minecraft.util.ResourceLocation;
import net.minecraftforge.fml.common.registry.ForgeRegistries;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
@SideOnly(Side.CLIENT)
public class EntityHelper
{
private static final Map<ResourceLocation, String> RESOURCELOCATION_TO_NAME;
private static final Map<Class<? extends Entity>, ResourceLocation> CLASS_TO_RESOURCELOCATION;
static
{
if(ForgeRegistries.ENTITIES.getEntries().isEmpty())
{
throw new RuntimeException("Accessed Entities before register!");
}
RESOURCELOCATION_TO_NAME = ForgeRegistries.ENTITIES.getEntries().stream().collect(Collectors.toMap(Entry::getKey, entry -> entry.getValue().getName()));
CLASS_TO_RESOURCELOCATION = ForgeRegistries.ENTITIES.getEntries().stream().collect(Collectors.toMap(entry -> entry.getValue().getEntityClass(), Entry::getKey));
}
@Nullable
public static String getUnifiedEntityName(String name)
{
for(ResourceLocation location : RESOURCELOCATION_TO_NAME.keySet())
{
if(RESOURCELOCATION_TO_NAME.get(location).equals(name))
{
return location.getResourcePath();
}
}
return null;
}
public static String getEntityName(ResourceLocation location)
{
return RESOURCELOCATION_TO_NAME.get(location);
}
@Nullable
public static String getEntityName(String name)
{
for(ResourceLocation location : RESOURCELOCATION_TO_NAME.keySet())
{
if(location.getResourcePath().equals(name))
{
return RESOURCELOCATION_TO_NAME.get(location);
}
}
return null;
}
public static boolean doesExist(ResourceLocation key)
{
return RESOURCELOCATION_TO_NAME.containsKey(key);
}
public static boolean doesExist(String value)
{
return RESOURCELOCATION_TO_NAME.containsValue(value);
}
@Nullable
public static ResourceLocation getResourceLocation(Class<? extends Entity> entity)
{
return CLASS_TO_RESOURCELOCATION.get(entity);
}
}

View File

@@ -0,0 +1,20 @@
package exopandora.worldhandler.helper;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
@SideOnly(Side.CLIENT)
public class EnumHelper
{
public static <T extends Enum<T>> T valueOf(Class<T> klass, String name)
{
try
{
return Enum.valueOf(klass, name.toUpperCase());
}
catch(Exception e)
{
return null;
}
}
}

View File

@@ -0,0 +1,280 @@
package exopandora.worldhandler.helper;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.function.BiFunction;
import javax.annotation.Nullable;
import net.minecraft.block.Block;
import net.minecraft.item.Item;
import net.minecraft.util.ResourceLocation;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
@SideOnly(Side.CLIENT)
public class Node
{
private final String key;
private List<Node> entries;
public Node()
{
this("root", null);
}
public Node(String key)
{
this(key, null);
}
public Node(String key, List<Node> entries)
{
this.key = key;
this.entries = entries;
}
public String getKey()
{
return this.key;
}
@Nullable
public List<Node> getEntries()
{
return this.entries;
}
public void addEntries(List<Node> entries)
{
this.validateEntries();
this.entries.addAll(entries);
}
public Node addNode(Node node)
{
this.validateEntries();
this.entries.add(node);
return node;
}
private void validateEntries()
{
if(this.entries == null)
{
this.entries = new ArrayList<Node>();
}
}
public Node addNode(String key)
{
return this.addNode(new Node(key));
}
public Node addNode(String key, List<Node> entries)
{
return this.addNode(new Node(key, entries));
}
public Node getNode(String key)
{
if(this.entries != null)
{
for(int x = 0; x < this.entries.size(); x++)
{
Node node = this.entries.get(x);
if(node.getKey().equals(key))
{
return node;
}
}
}
return null;
}
public void sort()
{
this.sort(this, (a, b) -> a.getKey().compareTo(b.getKey()));
}
public void sort(Comparator<Node> comparator)
{
this.sort(this, comparator);
}
private void sort(Node root, Comparator<Node> comparator)
{
if(root.getEntries() != null)
{
for(Node node : root.getEntries())
{
this.sort(node, comparator);
}
root.getEntries().sort(comparator);
}
}
public Node insertNode(String[] path)
{
return this.insertNode(path, this);
}
private Node insertNode(String[] path, Node root)
{
return this.insertNode(0, path, root);
}
private Node insertNode(int index, String[] path, Node root)
{
if(index == path.length)
{
return root;
}
Node node = new Node(path[index]);
if(root.getEntries() != null && root.getEntries().contains(node))
{
for(Node element : root.getEntries())
{
if(element.equals(node))
{
return this.insertNode(index + 1, path, element);
}
}
}
root.addNode(node);
return this.insertNode(index + 1, path, node);
}
public void mergeItems()
{
this.mergeItems(null, this);
}
private void mergeItems(Node root, Node child)
{
if(child != null && child.getEntries() != null)
{
if(root == null)
{
for(Node node : child.getEntries())
{
this.mergeItems(child, node);
}
}
else
{
if(child.getEntries() != null && !child.getEntries().isEmpty())
{
boolean flag = true;
for(Node node : child.getEntries())
{
if(node.getEntries() != null && !node.getEntries().isEmpty())
{
this.mergeItems(child, node);
flag = false;
break;
}
else
{
ResourceLocation location = new ResourceLocation(child.getKey(), node.getKey());
flag = flag && (Item.REGISTRY.containsKey(location) || Block.REGISTRY.containsKey(location));
}
}
if(flag)
{
this.merge(root, child.getKey(), (parent, kid) -> parent + ":" + kid);
}
}
}
}
}
public void merge(String key, BiFunction<String, String, String> merger)
{
this.merge(this, key, merger);
}
private void merge(Node root, String key, BiFunction<String, String, String> merger)
{
if(root.getEntries() != null)
{
Node node = root.getNode(key);
if(node != null)
{
root.getEntries().remove(node);
for(Node entry : node.getEntries())
{
root.addNode(new Node(merger.apply(node.getKey(), entry.getKey()), entry.getEntries()));
}
}
for(Node entry : root.getEntries())
{
this.merge(entry, key, merger);
}
}
}
public void print()
{
this.print("", this);
}
private void print(String offset, Node node)
{
System.out.println(offset + node.getKey());
if(node.getEntries() != null)
{
for(Node entry : node.getEntries())
{
this.print(offset + " ", entry);
}
}
}
public void writeFile(FileOutputStream out) throws IOException
{
this.writeFile("", this, out);
}
protected void writeFile(String offset, Node node, FileOutputStream out) throws IOException
{
out.write((offset + node.getKey() + "\n").getBytes());
if(node.getEntries() != null)
{
for(Node entry : node.getEntries())
{
this.writeFile(offset + " ", entry, out);
}
}
}
@Override
public boolean equals(Object obj)
{
if(obj instanceof Node)
{
Node node = (Node) obj;
return this.key.equals(node.getKey());
}
return false;
}
}

View File

@@ -0,0 +1,88 @@
package exopandora.worldhandler.helper;
import java.util.function.Predicate;
import javax.annotation.Nullable;
import exopandora.worldhandler.config.ConfigSkin;
import exopandora.worldhandler.main.Main;
import net.minecraft.block.Block;
import net.minecraft.entity.EntityList;
import net.minecraft.item.Item;
import net.minecraft.util.ResourceLocation;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
@SideOnly(Side.CLIENT)
public class ResourceHelper
{
private static final ResourceLocation BACKGROUND = new ResourceLocation("textures/gui/demo_background.png");
private static final ResourceLocation BACKGROUND_VANILLA = new ResourceLocation(Main.MODID, "textures/skins/vanilla/vanilla.png");
private static final ResourceLocation BUTTON = new ResourceLocation("textures/gui/widgets.png");
public static ResourceLocation stringToResourceLocation(String resource)
{
if(resource != null)
{
return new ResourceLocation(resource.replaceAll(" ", "_"));
}
return null;
}
public static boolean isRegisteredItem(String item)
{
return Item.REGISTRY.getKeys().contains(stringToResourceLocation(item));
}
public static boolean isRegisteredBlock(String block)
{
return Block.REGISTRY.getKeys().contains(stringToResourceLocation(block));
}
public static boolean isRegisteredMob(String mob)
{
return EntityList.isRegistered(stringToResourceLocation(mob));
}
public static boolean isRegisteredAdvancement(String advancement)
{
return AdvancementHelper.ADVANCEMENT_MANAGER.getAdvancement(stringToResourceLocation(advancement)) != null;
}
@Nullable
public static ResourceLocation stringToResourceLocationNullable(String resource, Predicate<String> predicate)
{
if(predicate.test(resource))
{
return stringToResourceLocation(resource);
}
return null;
}
public static ResourceLocation getBackgroundTexture()
{
if(ConfigSkin.getTextureType().equals("resourcepack"))
{
return BACKGROUND;
}
return BACKGROUND_VANILLA;
}
public static ResourceLocation getIconTexture()
{
return new ResourceLocation(Main.MODID, "textures/icons/icons" + ConfigSkin.getIconSize() + ".png");
}
public static ResourceLocation getButtonTexture()
{
if(ConfigSkin.getTextureType().equals("resourcepack"))
{
return BUTTON;
}
return new ResourceLocation(Main.MODID, "textures/skins/" + ConfigSkin.getTextureType() + "/" + ConfigSkin.getTextureType() + "_buttons.png");
}
}

View File

@@ -0,0 +1,117 @@
package exopandora.worldhandler.helper;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Function;
import java.util.function.Predicate;
import com.google.common.base.Predicates;
import com.mojang.realmsclient.gui.ChatFormatting;
import net.minecraft.scoreboard.IScoreCriteria;
import net.minecraft.scoreboard.Team.CollisionRule;
import net.minecraft.scoreboard.Team.EnumVisible;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
@SideOnly(Side.CLIENT)
public class ScoreboardHelper
{
private final Node objectives = new Node();
private final Node slots = new Node();
private final Node options = new Node();
public ScoreboardHelper()
{
this.init();
}
private void init()
{
//Lists
final List<Node> colors = this.createList(ChatFormatting.values(), ChatFormatting::getName, ChatFormatting::isColor);
final List<Node> visibility = this.createList(EnumVisible.values(), value -> value.internalName);
final List<Node> collision = this.createList(CollisionRule.values(), value -> value.name);
final List<Node> bool = this.createList(new Boolean[] {true, false}, String::valueOf);
//Objectives
for(String criteria : IScoreCriteria.INSTANCES.keySet())
{
this.objectives.insertNode(criteria.split("[.]"));
}
this.objectives.merge("stat", (parent, child) -> parent + "." + child);
this.objectives.mergeItems();
this.objectives.sort();
//Slots
this.slots.addNode("belowName");
this.slots.addNode("list");
this.slots.addNode("sidebar");
this.slots.addNode("sidebar.team", colors);
this.slots.sort();
//Options
this.options.addNode("color", colors);
this.options.addNode("nametagVisibility", visibility);
this.options.addNode("deathMessageVisibility", visibility);
this.options.addNode("friendlyfire", bool);
this.options.addNode("seeFriendlyInvisibles", bool);
this.options.addNode("collisionRule", collision);
this.options.sort();
}
private <T> List<Node> createList(T[] array, Function<T, String> mapper)
{
return this.createList(array, mapper, Predicates.<T>alwaysTrue());
}
private <T> List<Node> createList(T[] array, Function<T, String> mapper, Predicate<T> predicate)
{
List<Node> list = new ArrayList<Node>();
for(T index : array)
{
if(predicate.test(index))
{
list.add(new Node(mapper.apply(index)));
}
}
return list;
}
public List<Node> getObjectives()
{
if(this.objectives != null)
{
return this.objectives.getEntries();
}
return null;
}
public List<Node> getSlots()
{
if(this.slots != null)
{
return this.slots.getEntries();
}
return null;
}
public List<Node> getOptions()
{
if(this.options != null)
{
return this.options.getEntries();
}
return null;
}
}