Simplify project package structure

This commit is contained in:
Marcel Konrad
2019-11-12 18:35:05 +01:00
parent 12cc7f7079
commit ef37bd39d2
74 changed files with 141 additions and 145 deletions

View File

@@ -0,0 +1,204 @@
package exopandora.worldhandler.util;
import exopandora.worldhandler.Main;
import exopandora.worldhandler.WorldHandler;
import exopandora.worldhandler.builder.impl.BuilderDifficulty;
import exopandora.worldhandler.builder.impl.BuilderGamemode;
import exopandora.worldhandler.builder.impl.BuilderGamemode.EnumGamemode;
import exopandora.worldhandler.builder.impl.BuilderTime;
import exopandora.worldhandler.builder.impl.BuilderTime.EnumMode;
import exopandora.worldhandler.builder.impl.BuilderWeather;
import exopandora.worldhandler.builder.impl.BuilderWeather.EnumWeather;
import exopandora.worldhandler.config.Config;
import exopandora.worldhandler.gui.container.impl.GuiWorldHandler;
import exopandora.worldhandler.gui.content.Content;
import exopandora.worldhandler.gui.content.Contents;
import exopandora.worldhandler.gui.content.impl.ContentChild;
import exopandora.worldhandler.gui.content.impl.ContentContinue;
import net.minecraft.block.AbstractSignBlock;
import net.minecraft.block.NoteBlock;
import net.minecraft.client.Minecraft;
import net.minecraft.client.resources.I18n;
import net.minecraft.util.text.ChatType;
import net.minecraft.util.text.ITextComponent;
import net.minecraft.util.text.StringTextComponent;
import net.minecraft.util.text.Style;
import net.minecraft.util.text.TextFormatting;
import net.minecraft.util.text.TranslationTextComponent;
import net.minecraft.world.Difficulty;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;
@OnlyIn(Dist.CLIENT)
public class ActionHelper
{
public static void backToGame()
{
Minecraft.getInstance().displayGuiScreen(null);
Minecraft.getInstance().mouseHelper.grabMouse();
}
public static void back(Content content) throws Exception
{
if(content.getBackContent() != null)
{
Minecraft.getInstance().displayGuiScreen(new GuiWorldHandler(content.getBackContent()));
}
}
public static void changeTab(Content content, int index)
{
ActionHelper.tryRun(() -> ActionHelper.open(content.getCategory().getContent(index)));
}
public static void open(String id) throws Exception
{
if(id != null)
{
ActionHelper.open(Contents.getRegisteredContent(id));
}
}
public static void open(Content content) throws Exception
{
if(content != null && !(content instanceof ContentContinue))
{
if(content instanceof ContentChild && Minecraft.getInstance().currentScreen != null && Minecraft.getInstance().currentScreen instanceof GuiWorldHandler)
{
Minecraft.getInstance().displayGuiScreen(new GuiWorldHandler(((ContentChild) content).withParent(((GuiWorldHandler) Minecraft.getInstance().currentScreen).getContent())));
}
else
{
Minecraft.getInstance().displayGuiScreen(new GuiWorldHandler(content));
}
}
}
public static void timeDawn()
{
CommandHelper.sendCommand(new BuilderTime(EnumMode.SET, Config.getSettings().getDawn()));
}
public static void timeNoon()
{
CommandHelper.sendCommand(new BuilderTime(EnumMode.SET, Config.getSettings().getNoon()));
}
public static void timeSunset()
{
CommandHelper.sendCommand(new BuilderTime(EnumMode.SET, Config.getSettings().getSunset()));
}
public static void timeMidnight()
{
CommandHelper.sendCommand(new BuilderTime(EnumMode.SET, Config.getSettings().getMidnight()));
}
public static void weatherClear()
{
CommandHelper.sendCommand(new BuilderWeather(EnumWeather.CLEAR));
}
public static void weatherRain()
{
CommandHelper.sendCommand(new BuilderWeather(EnumWeather.RAIN));
}
public static void weatherThunder()
{
CommandHelper.sendCommand(new BuilderWeather(EnumWeather.THUNDER));
}
public static void difficultyPeaceful()
{
CommandHelper.sendCommand(new BuilderDifficulty(Difficulty.PEACEFUL));
}
public static void difficultyEasy()
{
CommandHelper.sendCommand(new BuilderDifficulty(Difficulty.EASY));
}
public static void difficultyNormal()
{
CommandHelper.sendCommand(new BuilderDifficulty(Difficulty.NORMAL));
}
public static void difficultyHard()
{
CommandHelper.sendCommand(new BuilderDifficulty(Difficulty.HARD));
}
public static void gamemodeSurvival()
{
CommandHelper.sendCommand(new BuilderGamemode(EnumGamemode.SURVIVAL));
}
public static void gamemodeCreative()
{
CommandHelper.sendCommand(new BuilderGamemode(EnumGamemode.CREATIVE));
}
public static void gamemodeAdventure()
{
CommandHelper.sendCommand(new BuilderGamemode(EnumGamemode.ADVENTURE));
}
public static void gamemodeSpectator()
{
CommandHelper.sendCommand(new BuilderGamemode(EnumGamemode.SPECTATOR));
}
public static void tryRun(ActionHandler action)
{
try
{
if(action != null)
{
action.run();
}
}
catch(Exception e)
{
Minecraft.getInstance().displayGuiScreen(null);
Minecraft.getInstance().mouseHelper.grabMouse();
Style redColor = new Style().setColor(TextFormatting.RED);
ITextComponent message = new TranslationTextComponent("<" + Main.NAME + "> %s", new TranslationTextComponent("worldhandler.error.gui")).setStyle(redColor);
ITextComponent cause = new StringTextComponent(" " + e.getClass().getCanonicalName() + ": " + e.getMessage()).setStyle(redColor);
Minecraft.getInstance().ingameGUI.addChatMessage(ChatType.SYSTEM, message);
Minecraft.getInstance().ingameGUI.addChatMessage(ChatType.SYSTEM, cause);
WorldHandler.LOGGER.throwing(e);
}
}
public static void displayGui()
{
if(!CommandHelper.canPlayerIssueCommand() && Config.getSettings().permissionQuery())
{
Minecraft.getInstance().ingameGUI.addChatMessage(ChatType.SYSTEM, new StringTextComponent(TextFormatting.RED + I18n.format("worldhandler.permission.refused")));
Minecraft.getInstance().ingameGUI.addChatMessage(ChatType.SYSTEM, new StringTextComponent(TextFormatting.RED + I18n.format("worldhandler.permission.refused.change", I18n.format("gui.worldhandler.config.settings.permission_query"))));
}
else
{
ActionHelper.tryRun(() ->
{
if(BlockHelper.getFocusedBlock() instanceof AbstractSignBlock)
{
Minecraft.getInstance().displayGuiScreen(new GuiWorldHandler(Contents.SIGN_EDITOR));
}
else if(BlockHelper.getFocusedBlock() instanceof NoteBlock)
{
Minecraft.getInstance().displayGuiScreen(new GuiWorldHandler(Contents.NOTE_EDITOR));
}
else
{
Minecraft.getInstance().displayGuiScreen(new GuiWorldHandler(Contents.MAIN));
}
});
}
}
}

View File

@@ -0,0 +1,52 @@
package exopandora.worldhandler.util;
import java.util.Collection;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Executor;
import net.minecraft.advancements.Advancement;
import net.minecraft.advancements.AdvancementManager;
import net.minecraft.client.Minecraft;
import net.minecraft.profiler.IProfiler;
import net.minecraft.resources.IFutureReloadListener;
import net.minecraft.resources.IResourceManager;
import net.minecraft.resources.ResourcePackInfo;
import net.minecraft.resources.ResourcePackType;
import net.minecraft.resources.SimpleReloadableResourceManager;
import net.minecraft.tags.NetworkTagManager;
import net.minecraft.util.Unit;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;
@OnlyIn(Dist.CLIENT)
public class AdvancementHelper implements IFutureReloadListener
{
private static final AdvancementHelper INSTANCE = new AdvancementHelper();
private final AdvancementManager manager = new AdvancementManager();
@Override
public CompletableFuture<Void> reload(IStage stage, IResourceManager resourceManager, IProfiler preparationsProfiler, IProfiler reloadProfiler, Executor backgroundExecutor, Executor gameExecutor)
{
return CompletableFuture.supplyAsync(() ->
{
SimpleReloadableResourceManager serverResourceManager = new SimpleReloadableResourceManager(ResourcePackType.SERVER_DATA, Thread.currentThread());
Minecraft.getInstance().getResourcePackList().getEnabledPacks().stream().map(ResourcePackInfo::getResourcePack).forEach(serverResourceManager::addResourcePack);
serverResourceManager.addReloadListener(new NetworkTagManager());
serverResourceManager.addReloadListener(this.manager);
return serverResourceManager;
}).thenCompose(stage::markCompleteAwaitingOthers).thenAcceptAsync(serverResourceManager ->
{
serverResourceManager.initialReload(backgroundExecutor, gameExecutor, CompletableFuture.completedFuture(Unit.INSTANCE));
});
}
public Collection<Advancement> getAdvancements()
{
return this.manager.getAllAdvancements();
}
public static AdvancementHelper getInstance()
{
return INSTANCE;
}
}

View File

@@ -0,0 +1,198 @@
package exopandora.worldhandler.util;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;
import java.util.function.Function;
import javax.annotation.Nonnull;
import org.apache.commons.lang3.ArrayUtils;
import exopandora.worldhandler.builder.impl.BuilderFill;
import exopandora.worldhandler.builder.impl.BuilderSetBlock;
import exopandora.worldhandler.builder.types.BlockResourceLocation;
import exopandora.worldhandler.builder.types.Coordinate.CoordinateType;
import exopandora.worldhandler.builder.types.CoordinateInt;
import exopandora.worldhandler.config.Config;
import net.minecraft.block.Block;
import net.minecraft.block.Blocks;
import net.minecraft.client.Minecraft;
import net.minecraft.network.play.client.CUpdateCommandBlockPacket;
import net.minecraft.state.properties.BlockStateProperties;
import net.minecraft.tileentity.CommandBlockTileEntity;
import net.minecraft.util.Direction;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.BlockRayTraceResult;
import net.minecraft.util.math.RayTraceResult;
import net.minecraft.util.math.RayTraceResult.Type;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;
@OnlyIn(Dist.CLIENT)
public class BlockHelper
{
private static BlockPos POS_1 = BlockPos.ZERO;
private static BlockPos POS_2 = BlockPos.ZERO;
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 result = Minecraft.getInstance().objectMouseOver;
if(result != null && result.getType().equals(Type.BLOCK))
{
BlockRayTraceResult blockResult = (BlockRayTraceResult) result;
if(!ArrayUtils.contains(BLACKLIST, Minecraft.getInstance().world.getBlockState(blockResult.getPos()).getBlock()))
{
return blockResult.getPos();
}
}
return Minecraft.getInstance().player.getPosition();
}
public static Block getFocusedBlock()
{
return Minecraft.getInstance().world.getBlockState(getFocusedBlockPos()).getBlock();
}
public static Block getBlock(BlockPos pos)
{
return Minecraft.getInstance().world.getBlockState(pos).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);
}
@Nonnull
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);
}
}
}
@Nonnull
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 <T> T addPositionObservers(T observer, Function<T, Consumer<BlockPos>> pos1generator, Function<T, Consumer<BlockPos>> pos2generator)
{
BlockHelper.addPos1Observer(pos1generator.apply(observer));
BlockHelper.addPos2Observer(pos2generator.apply(observer));
return observer;
}
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(CommandHelper.canPlayerIssueCommand() && Minecraft.getInstance().getConnection() != null)
{
BlockPos pos = Minecraft.getInstance().player.getPosition().add(0, 3, 0);
BuilderFill placeFill = new BuilderFill();
placeFill.setPosition1(pos);
placeFill.setPosition2(pos.up());
placeFill.setBlock1(new BlockResourceLocation(Blocks.COMMAND_BLOCK.getRegistryName()));
BuilderFill removeFill = new BuilderFill();
removeFill.setX1(new CoordinateInt(0, CoordinateType.GLOBAL));
removeFill.setY1(new CoordinateInt(-1, CoordinateType.GLOBAL));
removeFill.setZ1(new CoordinateInt(0, CoordinateType.GLOBAL));
removeFill.setX2(new CoordinateInt(0, CoordinateType.GLOBAL));
removeFill.setY2(new CoordinateInt(0, CoordinateType.GLOBAL));
removeFill.setZ2(new CoordinateInt(0, CoordinateType.GLOBAL));
removeFill.setBlock1(new BlockResourceLocation(Blocks.AIR.getRegistryName()));
Minecraft.getInstance().player.sendChatMessage(placeFill.toActualCommand());
Minecraft.getInstance().getConnection().sendPacket(new CUpdateCommandBlockPacket(pos, command, CommandBlockTileEntity.Mode.REDSTONE, true, false, true));
Minecraft.getInstance().getConnection().sendPacket(new CUpdateCommandBlockPacket(pos.up(), removeFill.toActualCommand(), CommandBlockTileEntity.Mode.REDSTONE, true, false, true));
return true;
}
return false;
}
public static void setBlockNearPlayer(Block block)
{
switch(Minecraft.getInstance().player.getHorizontalFacing())
{
case NORTH:
CommandHelper.sendCommand(new BuilderSetBlock(new CoordinateInt(CoordinateType.LOCAL), new CoordinateInt(CoordinateType.LOCAL), new CoordinateInt(2, CoordinateType.LOCAL), block.getRegistryName(), Config.getSettings().getBlockPlacingMode()).withState(BlockStateProperties.HORIZONTAL_FACING, Direction.SOUTH));
break;
case EAST:
CommandHelper.sendCommand(new BuilderSetBlock(new CoordinateInt(CoordinateType.LOCAL), new CoordinateInt(CoordinateType.LOCAL), new CoordinateInt(2, CoordinateType.LOCAL), block.getRegistryName(), Config.getSettings().getBlockPlacingMode()).withState(BlockStateProperties.HORIZONTAL_FACING, Direction.WEST));
break;
case SOUTH:
CommandHelper.sendCommand(new BuilderSetBlock(new CoordinateInt(CoordinateType.LOCAL), new CoordinateInt(CoordinateType.LOCAL), new CoordinateInt(2, CoordinateType.LOCAL), block.getRegistryName(), Config.getSettings().getBlockPlacingMode()).withState(BlockStateProperties.HORIZONTAL_FACING, Direction.NORTH));
break;
case WEST:
CommandHelper.sendCommand(new BuilderSetBlock(new CoordinateInt(CoordinateType.LOCAL), new CoordinateInt(CoordinateType.LOCAL), new CoordinateInt(2, CoordinateType.LOCAL), block.getRegistryName(), Config.getSettings().getBlockPlacingMode()).withState(BlockStateProperties.HORIZONTAL_FACING, Direction.EAST));
break;
default:
break;
}
}
}

View File

@@ -0,0 +1,68 @@
package exopandora.worldhandler.util;
import com.mojang.brigadier.CommandDispatcher;
import exopandora.worldhandler.WorldHandler;
import exopandora.worldhandler.builder.ICommandBuilder;
import exopandora.worldhandler.builder.ICommandBuilderSyntax;
import exopandora.worldhandler.command.CommandWH;
import exopandora.worldhandler.command.CommandWorldHandler;
import net.minecraft.client.Minecraft;
import net.minecraft.command.CommandSource;
import net.minecraft.util.text.StringTextComponent;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;
public class CommandHelper
{
public static void sendFeedback(CommandSource source, String message)
{
source.sendFeedback(new StringTextComponent(message), false);
}
public static boolean canPlayerIssueCommand()
{
return Minecraft.getInstance().player.hasPermissionLevel(1);
}
public static void registerCommands(CommandDispatcher<CommandSource> dispatcher)
{
CommandWorldHandler.register(dispatcher);
CommandWH.register(dispatcher);
}
@OnlyIn(Dist.CLIENT)
public static void sendCommand(ICommandBuilder builder)
{
CommandHelper.sendCommand(builder, false);
}
@OnlyIn(Dist.CLIENT)
public static void sendCommand(ICommandBuilder builder, boolean special)
{
if(builder != null)
{
String command;
if(builder instanceof ICommandBuilderSyntax)
{
command = ((ICommandBuilderSyntax) builder).toActualCommand();
}
else
{
command = builder.toCommand();
}
WorldHandler.LOGGER.info("Command: " + command);
if(builder.needsCommandBlock() || special)
{
BlockHelper.setCommandBlockNearPlayer(command);
}
else
{
Minecraft.getInstance().player.sendChatMessage(command);
}
}
}
}

View File

@@ -0,0 +1,23 @@
package exopandora.worldhandler.util;
import javax.annotation.Nullable;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;
@OnlyIn(Dist.CLIENT)
public class EnumHelper
{
@Nullable
public static <T extends Enum<T>> T valueOf(String name, Class<T> klass)
{
try
{
return Enum.valueOf(klass, name.toUpperCase());
}
catch(Exception e)
{
return null;
}
}
}

View File

@@ -0,0 +1,10 @@
package exopandora.worldhandler.util;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;
@OnlyIn(Dist.CLIENT)
public interface ILogic
{
String getId();
}

View File

@@ -0,0 +1,288 @@
package exopandora.worldhandler.util;
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 java.util.function.BiPredicate;
import javax.annotation.Nullable;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;
@OnlyIn(Dist.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, null);
}
public void mergeItems(BiPredicate<String, String> predicate)
{
this.mergeItems(null, this, predicate);
}
private void mergeItems(Node root, Node child, BiPredicate<String, String> predicate)
{
if(child != null && child.getEntries() != null)
{
if(root == null)
{
for(Node node : child.getEntries())
{
this.mergeItems(child, node, predicate);
}
}
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, predicate);
flag = false;
break;
}
else if(predicate != null)
{
flag = flag && predicate.test(child.getKey(), node.getKey());
}
}
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;
}
@Override
public String toString()
{
return this.getKey();
}
}

View File

@@ -0,0 +1,69 @@
package exopandora.worldhandler.util;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Function;
import javax.annotation.Nullable;
import exopandora.worldhandler.Main;
import net.minecraft.block.Block;
import net.minecraft.enchantment.Enchantment;
import net.minecraft.entity.EntityType;
import net.minecraft.item.Item;
import net.minecraft.potion.Effect;
import net.minecraft.util.ResourceLocation;
import net.minecraft.world.biome.Biome;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;
import net.minecraftforge.registries.ForgeRegistries;
import net.minecraftforge.registries.ForgeRegistryEntry;
import net.minecraftforge.registries.IForgeRegistry;
import net.minecraftforge.registries.IForgeRegistryEntry;
@OnlyIn(Dist.CLIENT)
public class RegistryHelper
{
private static final Map<IForgeRegistry<?>, Function<?, String>> FORGE = new HashMap<IForgeRegistry<?>, Function<?, String>>();
static
{
registerRegistry(ForgeRegistries.BLOCKS, Block::getTranslationKey);
registerRegistry(ForgeRegistries.ITEMS, Item::getTranslationKey);
registerRegistry(ForgeRegistries.POTIONS, Effect::getName);
registerRegistry(ForgeRegistries.BIOMES, Biome::getTranslationKey);
registerRegistry(ForgeRegistries.ENCHANTMENTS, Enchantment::getName);
registerRegistry(ForgeRegistries.ENTITIES, EntityType::getTranslationKey);
registerRegistry(ForgeRegistries.STAT_TYPES, stat -> "stat." + stat.toString().replace(':', '.'));
}
private static <T extends ForgeRegistryEntry<T>> void registerRegistry(IForgeRegistry<T> registry, Function<T, String> mapper)
{
FORGE.put(registry, mapper);
}
@Nullable
@SuppressWarnings("unchecked")
public static <T> String translate(ResourceLocation resource)
{
for(IForgeRegistry<?> registry : FORGE.keySet())
{
if(registry.containsKey(resource))
{
return ((Function<T, String>) FORGE.get(registry)).apply((T) registry.getValue(resource));
}
}
return null;
}
public static <T extends IForgeRegistryEntry<T>> void register(IForgeRegistry<T> registry, String name, T entry)
{
register(registry, Main.MODID, name, entry);
}
public static <T extends IForgeRegistryEntry<T>> void register(IForgeRegistry<T> registry, String modid, String name, T entry)
{
registry.register(entry.setRegistryName(new ResourceLocation(modid, name)));
}
}

View File

@@ -0,0 +1,105 @@
package exopandora.worldhandler.util;
import javax.annotation.Nullable;
import exopandora.worldhandler.Main;
import exopandora.worldhandler.builder.types.BlockResourceLocation;
import exopandora.worldhandler.builder.types.ItemResourceLocation;
import exopandora.worldhandler.config.Config;
import net.minecraft.util.ResourceLocation;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;
import net.minecraftforge.registries.IForgeRegistry;
@OnlyIn(Dist.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(stripToResourceLocation(resource));
}
return null;
}
@Nullable
public static String stripToResourceLocation(String resource)
{
if(resource != null)
{
return resource.toLowerCase().replaceAll("[^a-z0-9/._\\-:]", "_");
}
return null;
}
@Nullable
public static boolean isRegistered(ResourceLocation resource, IForgeRegistry<?> registry)
{
return resource != null && registry != null && registry.containsKey(resource);
}
@Nullable
public static boolean isRegistered(BlockResourceLocation resource, IForgeRegistry<?> registry)
{
if(resource != null)
{
return isRegistered(resource.getResourceLocation(), registry);
}
return false;
}
@Nullable
public static boolean isRegistered(ItemResourceLocation resource, IForgeRegistry<?> registry)
{
if(resource != null)
{
return isRegistered(resource.getResourceLocation(), registry);
}
return false;
}
@Nullable
public static ResourceLocation assertRegistered(ResourceLocation resource, IForgeRegistry<?> registry)
{
if(ResourceHelper.isRegistered(resource, registry))
{
return resource;
}
return null;
}
public static ResourceLocation getBackgroundTexture()
{
if(Config.getSkin().getTextureType().equals("resourcepack"))
{
return BACKGROUND;
}
return BACKGROUND_VANILLA;
}
public static ResourceLocation getIconTexture()
{
return new ResourceLocation(Main.MODID, "textures/icons/icons_" + Config.getSkin().getIconSize().name() + ".png");
}
public static ResourceLocation getButtonTexture()
{
if(Config.getSkin().getTextureType().equals("resourcepack"))
{
return BUTTON;
}
return new ResourceLocation(Main.MODID, "textures/skins/" + Config.getSkin().getTextureType() + "/" + Config.getSkin().getTextureType() + "_buttons.png");
}
}

View File

@@ -0,0 +1,141 @@
package exopandora.worldhandler.util;
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 net.minecraft.scoreboard.ScoreCriteria;
import net.minecraft.scoreboard.Team.CollisionRule;
import net.minecraft.scoreboard.Team.Visible;
import net.minecraft.stats.StatType;
import net.minecraft.stats.Stats;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.text.TextFormatting;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;
import net.minecraftforge.registries.ForgeRegistries;
@OnlyIn(Dist.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(TextFormatting.values(), TextFormatting::getFriendlyName, TextFormatting::isColor);
final List<Node> visibility = this.createList(Visible.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 scoreCriteria : ScoreCriteria.INSTANCES.keySet())
{
this.objectives.insertNode(scoreCriteria.split("[.:]"));
}
this.objectives.merge("minecraft", (parent, child) -> parent + "." + child);
for(StatType<?> type : ForgeRegistries.STAT_TYPES)
{
if(!type.equals(Stats.CUSTOM))
{
List<Node> entries = new ArrayList<Node>();
for(ResourceLocation key : type.getRegistry().keySet())
{
entries.add(new Node(this.buildKey(key)));
}
this.objectives.addNode(this.buildKey(ForgeRegistries.STAT_TYPES.getKey(type)), entries);
}
}
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 String buildKey(ResourceLocation key)
{
return key.toString().replace(":", ".");
}
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;
}
}

View File

@@ -3,7 +3,6 @@ package exopandora.worldhandler.util;
import com.mojang.blaze3d.platform.GlStateManager;
import exopandora.worldhandler.config.Config;
import exopandora.worldhandler.helper.ResourceHelper;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.screen.Screen;
import net.minecraftforge.api.distmarker.Dist;