Skip to content

Commit

Permalink
Add a value event to support MCDA
Browse files Browse the repository at this point in the history
  • Loading branch information
jaredlll08 committed Jul 19, 2022
1 parent 471f47b commit 0f6b7c4
Show file tree
Hide file tree
Showing 15 changed files with 199 additions and 1 deletion.
4 changes: 4 additions & 0 deletions Common/src/main/java/com/blamejared/clumps/ClumpsCommon.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,15 @@

import net.minecraft.world.entity.ExperienceOrb;
import net.minecraft.world.entity.player.Player;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.util.function.BiPredicate;

public class ClumpsCommon {

public static final Logger LOG = LogManager.getLogger("clumps");

public static BiPredicate<Player, ExperienceOrb> pickupXPEvent = (player, experienceOrb) -> false;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.blamejared.clumps.api.events;

public interface IEventHandler<T, U> {

U handle(T event);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.blamejared.clumps.api.events;

/**
* Used to mutate the value of experience before repairing player's items and giving the rest to the player.
*/
public interface IValueEvent {

/**
* Sets the value of the experience orb.
*
* @param value The new value to set.
*/
void setValue(int value);

/**
* Gets the value of the experience orb.
*
* @return the value of the experience orb.
*/
int getValue();

}
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package com.blamejared.clumps.mixin;

import com.blamejared.clumps.ClumpsCommon;
import com.blamejared.clumps.api.events.IValueEvent;
import com.blamejared.clumps.helper.IClumpedOrb;
import com.blamejared.clumps.platform.Services;
import com.mojang.datafixers.util.Either;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.world.entity.Entity;
Expand All @@ -24,6 +27,8 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.function.UnaryOperator;
import java.util.stream.Collectors;
import java.util.stream.Stream;

Expand Down Expand Up @@ -80,8 +85,11 @@ public void playerTouch(Player player, CallbackInfo ci) {
player.take(this, 1);

clumps$getClumpedMap().forEach((value, amount) -> {
Either<IValueEvent, Integer> result = Services.EVENT.fireValueEvent(value);
int actualValue = result.map(IValueEvent::getValue, UnaryOperator.identity());

for(int i = 0; i < amount; i++) {
int leftOver = this.repairPlayerItems(player, value);
int leftOver = this.repairPlayerItems(player, actualValue);
if(leftOver > 0) {
player.giveExperiencePoints(leftOver);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.blamejared.clumps.platform;


import com.blamejared.clumps.api.events.IValueEvent;
import com.mojang.datafixers.util.Either;

import java.util.Optional;

public interface IEventHelper {

Either<IValueEvent, Integer> fireValueEvent(int value);

}
20 changes: 20 additions & 0 deletions Common/src/main/java/com/blamejared/clumps/platform/Services.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.blamejared.clumps.platform;

import com.blamejared.clumps.ClumpsCommon;

import java.util.ServiceLoader;

public class Services {

public static final IEventHelper EVENT = load(IEventHelper.class);

public static <T> T load(Class<T> clazz) {

final T loadedService = ServiceLoader.load(clazz)
.findFirst()
.orElseThrow(() -> new NullPointerException("Failed to load service for " + clazz.getName()));
ClumpsCommon.LOG.debug("Loaded {} for service {}", loadedService, clazz);
return loadedService;
}

}
2 changes: 2 additions & 0 deletions Fabric/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ plugins {
val modVersion: String by project
val minecraftVersion: String by project
val fabricLoaderVersion: String by project
val fabricVersion: String by project
val modName: String by project
val modAuthor: String by project
val modId: String by project
Expand All @@ -36,6 +37,7 @@ dependencies {
parchment("org.parchmentmc.data:parchment-1.18.1:2021.12.19@zip")
})
modImplementation("net.fabricmc:fabric-loader:${fabricLoaderVersion}")
modImplementation("net.fabricmc.fabric-api:fabric-api:${fabricVersion}")
implementation(project(":Common"))
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.blamejared.clumps.api.events;

import net.fabricmc.fabric.api.event.Event;
import net.fabricmc.fabric.api.event.EventFactory;

public class ClumpsEvents {

public static final Event<IEventHandler<ValueEvent, Void>> VALUE_EVENT = EventFactory.createArrayBacked(IEventHandler.class, listeners -> event -> {
for(IEventHandler<ValueEvent, Void> listener : listeners) {
listener.handle(event);
}
return null;
});

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@

package com.blamejared.clumps.api.events;

/**
* Used to mutate the value of experience before repairing player's items and giving the rest to the player.
*/
public class ValueEvent implements IValueEvent {

private int value;

/**
* Sets the value of the experience orb.
*
* @param value The new value to set.
*/
@Override
public void setValue(int value) {

this.value = value;
}

/**
* Gets the value of the experience orb.
*
* @return the value of the experience orb.
*/
@Override
public int getValue() {

return value;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.blamejared.clumps.platform;

import com.blamejared.clumps.api.events.ClumpsEvents;
import com.blamejared.clumps.api.events.IValueEvent;
import com.blamejared.clumps.api.events.ValueEvent;
import com.mojang.datafixers.util.Either;
import net.fabricmc.loader.api.FabricLoader;

import java.util.Optional;

public class FabricEventHandler implements IEventHelper {

@Override
public Either<IValueEvent, Integer> fireValueEvent(int value) {

ValueEvent event = new ValueEvent();
if(FabricLoader.getInstance().isModLoaded("fabric")) {
ClumpsEvents.VALUE_EVENT.invoker().handle(event);
return Either.right(event.getValue());
}

return Either.right(event.getValue());
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
com.blamejared.clumps.platform.FabricEventHandler
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.blamejared.clumps.api.events;

import net.minecraftforge.eventbus.api.Event;

public class ValueEvent extends Event implements IValueEvent {

private int value;

public ValueEvent(int value) {

this.value = value;
}

@Override
public void setValue(int value) {

this.value = value;
}

@Override
public int getValue() {

return value;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.blamejared.clumps.platform;

import com.blamejared.clumps.api.events.IValueEvent;
import com.blamejared.clumps.api.events.ValueEvent;
import com.mojang.datafixers.util.Either;
import net.minecraftforge.common.MinecraftForge;

import java.util.Optional;

public class ForgeEventHandler implements IEventHelper {

@Override
public Either<IValueEvent, Integer> fireValueEvent(int value) {

ValueEvent event = new ValueEvent(value);
MinecraftForge.EVENT_BUS.post(event);
return Either.left(event);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
com.blamejared.clumps.platform.ForgeEventHandler
1 change: 1 addition & 0 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ minecraftVersion=1.18.2
forgeVersion=40.0.1

# Fabric
fabricVersion=0.57.0+1.18.2
fabricLoaderVersion=0.13.3

# Mod options
Expand Down

0 comments on commit 0f6b7c4

Please sign in to comment.