Skip to content

Commit

Permalink
Add Fabric support
Browse files Browse the repository at this point in the history
  • Loading branch information
IzzelAliz committed Feb 16, 2024
1 parent c524379 commit dd1f77e
Show file tree
Hide file tree
Showing 124 changed files with 2,799 additions and 558 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/gradle.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ jobs:
run: chmod +x gradlew
- name: Build
run: |
./gradlew cleanBuild build collect --no-daemon -i --stacktrace
./gradlew cleanBuild build collect --no-daemon --stacktrace
- name: Upload Artifact
uses: actions/upload-artifact@v2
with:
Expand Down
6 changes: 3 additions & 3 deletions arclight-common/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@ dependencies {
implementation "org.spigotmc:spigot-api:$minecraftVersion-R0.1-SNAPSHOT@jar"
implementation 'org.jetbrains:annotations:23.0.0'

implementation ('io.izzel.arclight:mixin-tools:1.1.0') { transitive = false }
annotationProcessor ('io.izzel.arclight:mixin-tools:1.1.0') { transitive = false }
implementation ("io.izzel.arclight:mixin-tools:$mixinToolsVersion") { transitive = false }
annotationProcessor ("io.izzel.arclight:mixin-tools:$mixinToolsVersion") { transitive = false }

implementation 'jline:jline:2.12.1'
implementation 'net.md-5:SpecialSource:1.11.0'
implementation 'net.md-5:SpecialSource:1.11.3'
implementation 'org.apache.logging.log4j:log4j-jul:2.15.0'
implementation 'net.md-5:bungeecord-chat:1.20-R0.1'
implementation 'mysql:mysql-connector-java:8.0.28'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@
import java.util.function.Function;

public interface CommandsBridge {
<S, T> void bridge$forge$mergeNode(CommandNode<S> sourceNode, CommandNode<T> resultNode,
default <S, T> void bridge$forge$mergeNode(CommandNode<S> sourceNode, CommandNode<T> resultNode,
Map<CommandNode<S>, CommandNode<T>> sourceToResult,
S canUse, Command<T> execute,
Function<SuggestionProvider<S>, SuggestionProvider<T>> sourceToResultSuggestion);
Function<SuggestionProvider<S>, SuggestionProvider<T>> sourceToResultSuggestion) {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
public interface ItemEntityBridge {

default int bridge$forge$onItemPickup(Player player) {
return 1;
return 0;
}

default void bridge$forge$firePlayerItemPickupEvent(Player player, ItemStack clone) {}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.izzel.arclight.common.bridge.core.util;

import net.minecraft.world.damagesource.DamageSource;
import net.minecraft.world.entity.Entity;

public interface DamageSourceBridge {

Expand All @@ -11,4 +12,16 @@ public interface DamageSourceBridge {
DamageSource bridge$poison();

DamageSource bridge$melting();

Entity bridge$getCausingEntity();

DamageSource bridge$customCausingEntity(Entity entity);

DamageSource bridge$setCustomCausingEntity(Entity entity);

org.bukkit.block.Block bridge$directBlock();

DamageSource bridge$directBlock(org.bukkit.block.Block block);

DamageSource bridge$setDirectBlock(org.bukkit.block.Block block);
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,13 @@
import net.minecraft.world.level.dimension.LevelStem;
import org.bukkit.craftbukkit.v.CraftServer;
import org.bukkit.craftbukkit.v.CraftWorld;
import org.bukkit.craftbukkit.v.block.CapturedBlockState;
import org.bukkit.entity.SpawnCategory;
import org.bukkit.generator.ChunkGenerator;
import org.spigotmc.SpigotWorldConfig;

import java.util.Map;

public interface WorldBridge extends IWorldWriterBridge, IWorldBridge, InjectLevelBridge {

CraftServer bridge$getServer();
Expand Down Expand Up @@ -50,6 +53,8 @@ public interface WorldBridge extends IWorldWriterBridge, IWorldBridge, InjectLev

boolean bridge$preventPoiUpdated();

void bridge$preventPoiUpdated(boolean b);

void bridge$forge$notifyAndUpdatePhysics(BlockPos pos, LevelChunk chunk, BlockState oldBlock, BlockState newBlock, int i, int j);

default boolean bridge$forge$onBlockPlace(BlockPos pos, LivingEntity livingEntity, Direction direction) {
Expand All @@ -69,4 +74,16 @@ public interface WorldBridge extends IWorldWriterBridge, IWorldBridge, InjectLev
default boolean bridge$forge$restoringBlockSnapshots() {
return false;
}

Map<BlockPos, CapturedBlockState> bridge$getCapturedBlockState();

Map<BlockPos, BlockEntity> bridge$getCapturedBlockEntity();

default void bridge$platform$startCaptureBlockBreak() {}

default boolean bridge$isCapturingBlockBreak() {
return false;
}

default void bridge$platform$endCaptureBlockBreak() {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,12 @@ public interface ItemStackBridge {

void bridge$convertStack(int version);

CompoundTag bridge$getForgeCaps();
default CompoundTag bridge$getForgeCaps() {
return null;
}

void bridge$setForgeCaps(CompoundTag caps);
default void bridge$setForgeCaps(CompoundTag caps) {
}

default boolean bridge$forge$hasCraftingRemainingItem() {
return ((ItemStack) (Object) this).getItem().hasCraftingRemainingItem();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package io.izzel.arclight.common.mixin.bukkit;

import com.google.common.base.Function;
import io.izzel.arclight.common.bridge.core.entity.EntityBridge;
import io.izzel.arclight.common.bridge.core.world.WorldBridge;
import io.izzel.arclight.common.bridge.core.util.DamageSourceBridge;
import io.izzel.arclight.common.mod.util.ArclightCaptures;
import io.izzel.arclight.common.mod.util.DistValidate;
import net.minecraft.core.BlockPos;
Expand All @@ -19,6 +18,7 @@
import org.bukkit.craftbukkit.v.block.CraftBlockState;
import org.bukkit.craftbukkit.v.block.CraftBlockStates;
import org.bukkit.craftbukkit.v.block.data.CraftBlockData;
import org.bukkit.craftbukkit.v.damage.CraftDamageSource;
import org.bukkit.craftbukkit.v.event.CraftEventFactory;
import org.bukkit.craftbukkit.v.util.CraftMagicNumbers;
import org.bukkit.entity.Item;
Expand All @@ -31,59 +31,50 @@
import org.bukkit.event.block.EntityBlockFormEvent;
import org.bukkit.event.block.NotePlayEvent;
import org.bukkit.event.entity.EntityChangeBlockEvent;
import org.bukkit.event.entity.EntityDamageByEntityEvent;
import org.bukkit.event.entity.EntityDamageEvent;
import org.bukkit.event.entity.ItemSpawnEvent;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Overwrite;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.ModifyVariable;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;

import javax.annotation.Nullable;
import java.util.Map;

@Mixin(value = CraftEventFactory.class, remap = false)
public class CraftEventFactoryMixin {
public abstract class CraftEventFactoryMixin {

@Shadow public static Entity entityDamage;
@Shadow public static Block blockDamage;
// @formatter:off
@Shadow private static EntityDamageEvent callEntityDamageEvent(Entity damager, Entity damagee, EntityDamageEvent.DamageCause cause, org.bukkit.damage.DamageSource bukkitDamageSource, Map<EntityDamageEvent.DamageModifier, Double> modifiers, Map<EntityDamageEvent.DamageModifier, Function<? super Double, Double>> modifierFunctions, boolean cancelled) { return null; }
// @formatter:on

@Inject(method = "handleEntityDamageEvent(Lnet/minecraft/world/entity/Entity;Lnet/minecraft/world/damagesource/DamageSource;Ljava/util/Map;Ljava/util/Map;Z)Lorg/bukkit/event/entity/EntityDamageEvent;", at = @At("HEAD"))
private static void arclight$captureSource(Entity entity, DamageSource source, Map<EntityDamageEvent.DamageModifier, Double> modifiers, Map<EntityDamageEvent.DamageModifier, Function<? super Double, Double>> modifierFunctions, boolean cancelled, CallbackInfoReturnable<EntityDamageEvent> cir) {
@ModifyVariable(method = "handleEntityDamageEvent*", at = @At("HEAD"), index = 1, argsOnly = true)
private static DamageSource arclight$captureSource(DamageSource source, Entity entity) {
Entity damageEventEntity = ArclightCaptures.getDamageEventEntity();
BlockPos damageEventBlock = ArclightCaptures.getDamageEventBlock();
if (damageEventEntity != null && entityDamage == null) {
if (damageEventEntity != null && ((DamageSourceBridge) source).bridge$getCausingEntity() == null) {
if (source.is(DamageTypes.LIGHTNING_BOLT)) {
entityDamage = damageEventEntity;
source = ((DamageSourceBridge) source).bridge$customCausingEntity(damageEventEntity);
}
}
if (damageEventBlock != null && blockDamage == null) {
if (damageEventBlock != null && ((DamageSourceBridge) source).bridge$directBlock() == null) {
if (source.is(DamageTypes.CACTUS)
|| source.is(DamageTypes.SWEET_BERRY_BUSH)
|| source.is(DamageTypes.HOT_FLOOR)) {
blockDamage = CraftBlock.at(entity.getCommandSenderWorld(), damageEventBlock);
|| source.is(DamageTypes.SWEET_BERRY_BUSH)
|| source.is(DamageTypes.HOT_FLOOR)) {
source = ((DamageSourceBridge) source).bridge$directBlock(CraftBlock.at(entity.getCommandSenderWorld(), damageEventBlock));
}
}
return source;
}

@Inject(method = "handleEntityDamageEvent(Lnet/minecraft/world/entity/Entity;Lnet/minecraft/world/damagesource/DamageSource;Ljava/util/Map;Ljava/util/Map;Z)Lorg/bukkit/event/entity/EntityDamageEvent;", cancellable = true, at = @At(value = "NEW", target = "java/lang/IllegalStateException"))
@Inject(method = "handleEntityDamageEvent*", cancellable = true, at = @At(value = "NEW", target = "java/lang/IllegalStateException"))
private static void arclight$unhandledDamage(Entity entity, DamageSource source, Map<EntityDamageEvent.DamageModifier, Double> modifiers, Map<EntityDamageEvent.DamageModifier, Function<? super Double, Double>> modifierFunctions, boolean cancelled, CallbackInfoReturnable<EntityDamageEvent> cir) {
// todo blockDamage is lost
EntityDamageEvent event;
if (source.getEntity() != null) {
// ArclightMod.LOGGER.debug("Unhandled damage of {} by {} from {}", entity, source.getEntity(), source.msgId);
event = new EntityDamageByEntityEvent(((EntityBridge) source.getEntity()).bridge$getBukkitEntity(), ((EntityBridge) entity).bridge$getBukkitEntity(), EntityDamageEvent.DamageCause.CUSTOM, modifiers, modifierFunctions);
} else {
// ArclightMod.LOGGER.debug("Unhandled damage of {} from {}", entity, source.msgId);
event = new EntityDamageEvent(((EntityBridge) entity).bridge$getBukkitEntity(), EntityDamageEvent.DamageCause.CUSTOM, modifiers, modifierFunctions);
}
event.setCancelled(cancelled);
Bukkit.getPluginManager().callEvent(event);
if (!event.isCancelled()) {
((EntityBridge) entity).bridge$getBukkitEntity().setLastDamageCause(event);
}
CraftDamageSource bukkitDamageSource = new CraftDamageSource(source);
EntityDamageEvent event = callEntityDamageEvent(((DamageSourceBridge) source).bridge$getCausingEntity(), entity, EntityDamageEvent.DamageCause.CUSTOM, bukkitDamageSource, modifiers, modifierFunctions, cancelled);
cir.setReturnValue(event);
}

Expand Down Expand Up @@ -122,7 +113,7 @@ public static boolean handleBlockGrowEvent(Level world, BlockPos pos, net.minecr
world.setBlock(pos, newData, flag);
return true;
}
Block block = ((WorldBridge) world).bridge$getWorld().getBlockAt(pos.getX(), pos.getY(), pos.getZ());
Block block = world.bridge$getWorld().getBlockAt(pos.getX(), pos.getY(), pos.getZ());
CraftBlockState state = (CraftBlockState) block.getState();
state.setData(newData);

Expand Down Expand Up @@ -150,7 +141,7 @@ public static boolean handleBlockFormEvent(Level world, BlockPos pos, net.minecr
CraftBlockState blockState = CraftBlockStates.getBlockState(world, pos, flag);
blockState.setData(block);

BlockFormEvent event = (entity == null) ? new BlockFormEvent(blockState.getBlock(), blockState) : new EntityBlockFormEvent(((EntityBridge) entity).bridge$getBukkitEntity(), blockState.getBlock(), blockState);
BlockFormEvent event = (entity == null) ? new BlockFormEvent(blockState.getBlock(), blockState) : new EntityBlockFormEvent(entity.bridge$getBukkitEntity(), blockState.getBlock(), blockState);
Bukkit.getPluginManager().callEvent(event);

if (!event.isCancelled()) {
Expand Down Expand Up @@ -200,7 +191,7 @@ public static BlockPhysicsEvent callBlockPhysicsEvent(LevelAccessor world, Block
@Overwrite
public static boolean callEntityChangeBlockEvent(Entity entity, BlockPos position, net.minecraft.world.level.block.state.BlockState newBlock, boolean cancelled) {
Block block = CraftBlock.at(entity.level(), position);
EntityChangeBlockEvent event = new EntityChangeBlockEvent(((EntityBridge) entity).bridge$getBukkitEntity(), block, CraftBlockData.fromData(newBlock));
EntityChangeBlockEvent event = new EntityChangeBlockEvent(entity.bridge$getBukkitEntity(), block, CraftBlockData.fromData(newBlock));
event.setCancelled(cancelled);
// Suppress during worldgen
if (DistValidate.isValid(entity.level())) {
Expand Down Expand Up @@ -240,7 +231,7 @@ public static NotePlayEvent callNotePlayEvent(Level world, BlockPos pos, NoteBlo
@Inject(method = "callItemSpawnEvent", cancellable = true, at = @At("HEAD"))
private static void arclight$noAirDrops(ItemEntity itemEntity, CallbackInfoReturnable<ItemSpawnEvent> cir) {
if (itemEntity.getItem().isEmpty()) {
Item entity = (Item) ((EntityBridge) itemEntity).bridge$getBukkitEntity();
Item entity = (Item) itemEntity.bridge$getBukkitEntity();
ItemSpawnEvent event = new ItemSpawnEvent(entity);
event.setCancelled(true);
cir.setReturnValue(event);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import org.spongepowered.asm.mixin.Overwrite;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Desc;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;

Expand All @@ -30,7 +31,8 @@ public abstract class CraftItemStackMixin implements CraftItemStackBridge {
@Shadow static Material getType(ItemStack item) { throw new RuntimeException(); }
// @formatter:on

@Inject(method = "getItemMeta(Lnet/minecraft/world/item/ItemStack;)Lorg/bukkit/inventory/meta/ItemMeta;", cancellable = true, at = @At(value = "INVOKE", target = "Lorg/bukkit/Material;ordinal()I"))
@Desc(id = "getItemMeta", value = "getItemMeta", ret = ItemMeta.class, args = {ItemStack.class})
@Inject(method = "@Desc(getItemMeta)", cancellable = true, at = @At(value = "INVOKE", target = "Lorg/bukkit/Material;ordinal()I"))
private static void arclight$noTag(ItemStack item, CallbackInfoReturnable<ItemMeta> cir) {
if (item.getTag() == null) {
var meta = CraftItemFactory.instance().getItemMeta(getType(item));
Expand All @@ -39,7 +41,8 @@ public abstract class CraftItemStackMixin implements CraftItemStackBridge {
}
}

@Inject(method = "getItemMeta(Lnet/minecraft/world/item/ItemStack;)Lorg/bukkit/inventory/meta/ItemMeta;", at = @At("RETURN"))
@Desc(id = "getItemMeta", value = "getItemMeta", ret = ItemMeta.class, args = {ItemStack.class})
@Inject(method = "@Desc(getItemMeta)", at = @At("RETURN"))
private static void arclight$offerCaps(ItemStack item, CallbackInfoReturnable<ItemMeta> cir) {
if (item == null) return;
ItemMeta meta = cir.getReturnValue();
Expand All @@ -50,7 +53,8 @@ public abstract class CraftItemStackMixin implements CraftItemStackBridge {
((ItemMetaBridge) meta).bridge$setForgeCaps(((ItemStackBridge) (Object) item).bridge$getForgeCaps());
}

@Inject(method = "setItemMeta(Lnet/minecraft/world/item/ItemStack;Lorg/bukkit/inventory/meta/ItemMeta;)Z", at = @At(value = "INVOKE", ordinal = 1, remap = true, target = "Lnet/minecraft/world/item/ItemStack;getItem()Lnet/minecraft/world/item/Item;"))
@Desc(id = "setItemMeta", value = "setItemMeta", args = {ItemStack.class, ItemMeta.class}, ret = boolean.class)
@Inject(method = "@Desc(setItemMeta)", at = @At(value = "INVOKE", ordinal = 1, remap = true, target = "Lnet/minecraft/world/item/ItemStack;getItem()Lnet/minecraft/world/item/Item;"))
private static void arclight$setCaps(ItemStack item, ItemMeta itemMeta, CallbackInfoReturnable<Boolean> cir) {
CompoundTag forgeCaps = ((ItemMetaBridge) itemMeta).bridge$getForgeCaps();
if (forgeCaps != null) {
Expand All @@ -62,7 +66,7 @@ public abstract class CraftItemStackMixin implements CraftItemStackBridge {
* @author IzzelAliz
* @reason
*/
@Overwrite
@Overwrite(remap = false)
public boolean isSimilar(org.bukkit.inventory.ItemStack stack) {
if (stack == null) {
return false;
Expand Down Expand Up @@ -91,7 +95,8 @@ public boolean isSimilar(org.bukkit.inventory.ItemStack stack) {
: !that.hasItemMeta();
}

@Inject(method = "hasItemMeta(Lnet/minecraft/world/item/ItemStack;)Z", cancellable = true, at = @At("HEAD"))
@Desc(id = "hasItemMeta", value = "hasItemMeta", args = ItemStack.class, ret = boolean.class)
@Inject(method = "@Desc(hasItemMeta)", cancellable = true, at = @At("HEAD"))
private static void arclight$hasMeta(ItemStack item, CallbackInfoReturnable<Boolean> cir) {
if (item != null) {
CompoundTag forgeCaps = ((ItemStackBridge) (Object) item).bridge$getForgeCaps();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Desc;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.ModifyVariable;
import org.spongepowered.asm.mixin.injection.Redirect;
Expand All @@ -31,7 +32,7 @@
public class CraftMetaItemMixin implements ItemMetaBridge {

// @formatter:off
@Shadow(remap = false) @Final private Map<String, Tag> unhandledTags;
@Shadow(remap = false) @Final Map<String, Tag> unhandledTags;
@Shadow(remap = false) private CompoundTag internalTag;
// @formatter:on

Expand Down Expand Up @@ -65,7 +66,8 @@ public class CraftMetaItemMixin implements ItemMetaBridge {
"instrument"
);

@ModifyVariable(method = "<init>(Lnet/minecraft/nbt/CompoundTag;)V", at = @At(value = "INVOKE", target = "Lorg/bukkit/UnsafeValues;getDataVersion()I"))
@Desc(id = "compoundInit", value = "<init>", args = CompoundTag.class)
@ModifyVariable(method = "@Desc(compoundInit)", at = @At(value = "INVOKE", target = "Lorg/bukkit/UnsafeValues;getDataVersion()I"), argsOnly = true)
private CompoundTag arclight$provideTag(CompoundTag tag) {
return tag == null ? new CompoundTag() : tag;
}
Expand All @@ -77,8 +79,7 @@ public class CraftMetaItemMixin implements ItemMetaBridge {
// force internal tags to be deserialized into item nbt to avoid their vanilla tags being ignored by Bukkit.
// e.g. apotheosis:potion_charm{"Potion": "<effect id>"} or minecraft:bread{"Potion": "<effect id>"}
return false;
}
else {
} else {
// For items that has corresponding ItemMeta representation in Bukkit,
// keep their behavior unchanged.
return handledTags.contains((String) key);
Expand Down Expand Up @@ -127,7 +128,7 @@ public class CraftMetaItemMixin implements ItemMetaBridge {
}
}

@Inject(method = "clone", locals = LocalCapture.CAPTURE_FAILHARD, at = @At("RETURN"))
@Inject(method = "clone*", locals = LocalCapture.CAPTURE_FAILHARD, at = @At("RETURN"))
private void arclight$cloneTags(CallbackInfoReturnable<CraftMetaItem> cir, CraftMetaItem clone) {
if (this.unhandledTags != null) {
((ItemMetaBridge) clone).bridge$getUnhandledTags().putAll(this.unhandledTags);
Expand Down
Loading

0 comments on commit dd1f77e

Please sign in to comment.