Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MCFunctionLoadEvent #68

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
--- before/net/minecraft/advancements/FunctionManager.java
+++ after/net/minecraft/advancements/FunctionManager.java
@@ -182,6 +182,7 @@
}
}
}
+ net.minecraftforge.common.ForgeHooks.onMCFunctionLoad(this, this.field_193070_d);

if (!this.field_193070_d.isEmpty())
{
6 changes: 6 additions & 0 deletions src/main/java/net/minecraftforge/common/ForgeHooks.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,15 @@

import net.minecraft.advancements.Advancement;
import net.minecraft.advancements.AdvancementManager;
import net.minecraft.advancements.FunctionManager;
import net.minecraft.block.Block;
import net.minecraft.block.BlockFarmland;
import net.minecraft.block.BlockLiquid;
import net.minecraft.block.material.Material;
import net.minecraft.block.state.IBlockState;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.GuiScreen;
import net.minecraft.command.FunctionObject;
import net.minecraft.enchantment.Enchantment;
import net.minecraft.enchantment.EnchantmentHelper;
import net.minecraft.entity.Entity;
Expand Down Expand Up @@ -122,6 +124,7 @@
import net.minecraftforge.event.AnvilUpdateEvent;
import net.minecraftforge.event.DifficultyChangeEvent;
import net.minecraftforge.event.ForgeEventFactory;
import net.minecraftforge.event.MCFunctionLoadEvent;
import net.minecraftforge.event.ServerChatEvent;
import net.minecraftforge.event.entity.EntityTravelToDimensionEvent;
import net.minecraftforge.event.entity.item.ItemTossEvent;
Expand Down Expand Up @@ -1402,6 +1405,9 @@ private static boolean loadAdvancements(Map<ResourceLocation, Advancement.Builde
true, true
);
}
public static void onMCFunctionLoad(FunctionManager manager, Map<ResourceLocation, FunctionObject> functionsIn){
MinecraftForge.EVENT_BUS.post(new MCFunctionLoadEvent(manager, functionsIn));
}

public static void sendRecipeBook(NetHandlerPlayServer connection, State state, List<IRecipe> recipes, List<IRecipe> display, boolean isGuiOpen, boolean isFilteringCraftable)
{
Expand Down
222 changes: 222 additions & 0 deletions src/main/java/net/minecraftforge/event/MCFunctionLoadEvent.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,222 @@
package net.minecraftforge.event;


import com.google.common.collect.Lists;
import com.google.common.io.ByteSource;
import com.google.common.io.LineProcessor;
import net.minecraft.advancements.FunctionManager;
import net.minecraft.command.FunctionObject;
import net.minecraft.util.ResourceLocation;
import net.minecraft.world.EnumDifficulty;
import net.minecraftforge.common.ForgeHooks;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.fml.common.FMLLog;
import net.minecraftforge.fml.common.Loader;
import net.minecraftforge.fml.common.ModContainer;
import net.minecraftforge.fml.common.ObfuscationReflectionHelper;
import net.minecraftforge.fml.common.eventhandler.Cancelable;
import net.minecraftforge.fml.common.eventhandler.Event;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.io.IOUtils;

import javax.annotation.Nullable;
import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.FileSystem;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.*;
import java.util.function.BiFunction;
import java.util.function.Function;

/**
* MCFunctionLoadEvent is fired when {@link FunctionObject} `.mcfunction` be loaded and registered <br>
* <br>
* This event is fired via the {@link ForgeHooks#onMCFunctionLoad(FunctionManager, Map)}.<br>
* <br>
* This event is not {@link Cancelable}.<br>
* <br>
* This event does not have a result. {@link HasResult}<br>
* <br>
* This event is fired on the {@link MinecraftForge#EVENT_BUS}.
**/
public class MCFunctionLoadEvent extends Event {
public final FunctionManager functionManager;
public final Map<ResourceLocation, FunctionObject> functions;

/**
* @param manager the instance of {@link FunctionManager} , which is the owner of Functions.
* @param functionsIn the instance of {@link FunctionObject} map. It is a registry.
*/
public MCFunctionLoadEvent(FunctionManager manager, Map<ResourceLocation, FunctionObject> functionsIn){
this.functionManager = manager;
this.functions = functionsIn;
}

/**
* It will scan the `.mcfunction` file at `assets/<modid>/functions` and it's child folds, to load {@link FunctionObject} and register it, using the path as the register name.
* @param modid the modID
* @return the loaded {@link FunctionObject}
*/
public HashMap<ResourceLocation,FunctionObject> loadAndRegisterFor(String modid){
HashMap<ResourceLocation,FunctionObject> map = new HashMap<>();
ModContainer container = Loader.getLoadedMod(modid);
Loader.instance().setActiveModContainer(container);
findFiles(container, "assets/" + container.getModId() + "/functions",null,
(root, file) -> {
String relative = root.relativize(file).toString();
if (!"mcfunction".equals(FilenameUtils.getExtension(file.toString())) || relative.startsWith("_"))
return true;
String name = FilenameUtils.removeExtension(relative).replaceAll("\\\\", "/");
ResourceLocation key = new ResourceLocation(container.getModId(), name);
try {

FunctionObject functionObject = FunctionObject.create(
functionManager,
getByteSource(file).asCharSource(StandardCharsets.UTF_8)
.readLines(new LineProcessor<>() {
final List<String> result = Lists.newArrayList();

@Override
public boolean processLine(String line) {
result.add(line);
return true;
}

@Override
public List<String> getResult() {
return result;
}
})
);
map.put(key, functionObject);
return true;
} catch (IOException e) {
FMLLog.log.error("Couldn't read function {} from {}", key, file, e);
return false;
}
},true, true);
functions.putAll(map);
Loader.instance().setActiveModContainer(null);
return map;
}

/**
* @param resourceLocation the register name
* @param functionObject the function
*/
public void register(ResourceLocation resourceLocation, FunctionObject functionObject){
functions.put(resourceLocation, functionObject);
}

/**
* @param resourceLocation the register name
* @return the function
*/
@Nullable
public FunctionObject unregister(ResourceLocation resourceLocation){
return functions.remove(resourceLocation);
}

/**
* @param collection the register names
* @return the unregistered {@link FunctionObject}
*/
public HashMap<ResourceLocation,FunctionObject> unregisterAll(Collection<ResourceLocation> collection){
HashMap<ResourceLocation,FunctionObject> hashMap = new HashMap<>();
for(ResourceLocation resourceLocation:collection){
hashMap.put(resourceLocation, unregister(resourceLocation));
}
return hashMap;
}
private static boolean findFiles(ModContainer mod, String base, Function<Path, Boolean> preprocessor, BiFunction<Path, Path, Boolean> processor,
boolean defaultUnfoundRoot, boolean visitAllFiles)
{

File source = mod.getSource();

if ("minecraft".equals(mod.getModId()))
{
return true;
}

FileSystem fs = null;
boolean success = true;

try
{
Path root = null;

if (source.isFile())
{
try
{
fs = FileSystems.newFileSystem(source.toPath(), (ClassLoader)null);
root = fs.getPath("/" + base);
}
catch (IOException e)
{
FMLLog.log.error("Error loading FileSystem from jar: ", e);
return false;
}
}
else if (source.isDirectory())
{
root = source.toPath().resolve(base);
}

if (root == null || !Files.exists(root))
return defaultUnfoundRoot;

if (preprocessor != null)
{
Boolean cont = preprocessor.apply(root);
if (cont == null || !cont)
return false;
}

if (processor != null)
{
Iterator<Path> itr;
try
{
itr = Files.walk(root).iterator();
}
catch (IOException e)
{
FMLLog.log.error("Error iterating filesystem for: {}", mod.getModId(), e);
return false;
}

while (itr.hasNext())
{
Boolean cont = processor.apply(root, itr.next());

if (visitAllFiles)
{
success &= cont != null && cont;
}
else if (cont == null || !cont)
{
return false;
}
}
}
}
finally
{
IOUtils.closeQuietly(fs);
}

return success;
}
private static ByteSource getByteSource(Path path) throws IOException {
return ByteSource.wrap(IOUtils.toByteArray(Files.newBufferedReader(path), StandardCharsets.UTF_8));
}
public static FunctionObject singleFunction(FunctionObject.Entry entry){
return new FunctionObject(new FunctionObject.Entry[]{entry});
}
}
3 changes: 3 additions & 0 deletions src/main/java/net/minecraftforge/fml/common/Loader.java
Original file line number Diff line number Diff line change
Expand Up @@ -689,6 +689,9 @@ public static boolean isModLoaded(String modname)
{
return instance().namedMods.containsKey(modname) && instance().modController.getModState(instance.namedMods.get(modname))!=ModState.DISABLED;
}
public static ModContainer getLoadedMod(String modname){
return instance().namedMods.get(modname);
}

public File getConfigDir()
{
Expand Down