Skip to content

Commit

Permalink
1.20 part 3: make it work
Browse files Browse the repository at this point in the history
  • Loading branch information
mcmonkey4eva committed Jun 7, 2023
1 parent a6c9268 commit eba81d1
Show file tree
Hide file tree
Showing 17 changed files with 96 additions and 86 deletions.
2 changes: 2 additions & 0 deletions plugin/pom.xml
Expand Up @@ -12,6 +12,8 @@

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<BUILD_NUMBER>Unknown</BUILD_NUMBER>
<BUILD_CLASS>CUSTOM</BUILD_CLASS>
</properties>

<!-- Dependencies -->
Expand Down
Expand Up @@ -5,7 +5,8 @@ public enum NMSVersion {
NOT_SUPPORTED,
v1_17,
v1_18,
v1_19;
v1_19,
v1_20;

public boolean isAtLeast(NMSVersion version) {
return ordinal() >= version.ordinal();
Expand Down
Expand Up @@ -9,7 +9,7 @@ public class ReflectionMappingsInfo {
public static String AdvancementList_tasks = "d";

// net.minecraft.world.level.block.state.BlockBehaviour
public static String BlockBehaviour_explosionResistance = "aH";
public static String BlockBehaviour_explosionResistance = "aF";

// net.minecraft.core.MappedRegistry
public static String MappedRegistry_frozen = "l";
Expand All @@ -18,28 +18,28 @@ public class ReflectionMappingsInfo {
public static String RecipeManager_byName = "d";

// net.minecraft.world.entity.Entity
public static String Entity_onGround = "N";
public static String Entity_onGround = "aJ";
public static String Entity_DATA_SHARED_FLAGS_ID = "an";
public static String Entity_DATA_CUSTOM_NAME = "aR";
public static String Entity_DATA_CUSTOM_NAME_VISIBLE = "aS";
public static String Entity_DATA_CUSTOM_NAME = "aU";
public static String Entity_DATA_CUSTOM_NAME_VISIBLE = "aV";

// net.minecraft.world.entity.LivingEntity
public static String LivingEntity_attackStrengthTicker = "aO";
public static String LivingEntity_autoSpinAttackTicks = "bx";
public static String LivingEntity_attackStrengthTicker = "aQ";
public static String LivingEntity_autoSpinAttackTicks = "bz";
public static String LivingEntity_setLivingEntityFlag_method = "c";

// net.minecraft.world.entity.player.Player
public static String Player_DATA_PLAYER_ABSORPTION_ID = "e";
public static String Player_DATA_PLAYER_MODE_CUSTOMISATION = "bJ";
public static String Player_DATA_PLAYER_MODE_CUSTOMISATION = "bL";

// net.minecraft.server.level.ServerPlayer
public static String ServerPlayer_respawnForced = "cP";
public static String ServerPlayer_respawnForced = "cQ";

// net.minecraft.world.entity.monster.EnderMan
public static String EnderMan_DATA_CREEPY = "bU";
public static String EnderMan_DATA_CREEPY = "bV";

// net.minecraft.world.entity.monster.Zombie
public static String Zombie_inWaterTime = "cc";
public static String Zombie_inWaterTime = "cd";

// net.minecraft.world.item.Item
public static String Item_maxStackSize = "d";
Expand Down Expand Up @@ -114,7 +114,7 @@ public class ReflectionMappingsInfo {
public static String FishingHook_timeUntilHooked = "l";

// net.minecraft.world.level.block.state.BlockBehaviour$BlockStateBase
public static String BlockBehaviourBlockStateBase_getFluidState_method = "r";
public static String BlockBehaviourBlockStateBase_getFluidState_method = "u";

// net.minecraft.world.level.material.FluidState
public static String FluidState_isRandomlyTicking_method = "f";
Expand All @@ -132,6 +132,6 @@ public class ReflectionMappingsInfo {
public static String HolderReference_bindTags_method = "a";

// net.minecraft.server.level.ServerLevel
public static String ServerLevel_sleepStatus = "N";
public static String ServerLevel_sleepStatus = "O";

}
Expand Up @@ -192,7 +192,7 @@ private static Advancement asNMSCopy(com.denizenscript.denizen.nms.util.Advancem
requirements[i] = new String[] { IMPOSSIBLE_KEY + i };
}
}
return new Advancement(key, parent, display, AdvancementRewards.EMPTY, criteria, requirements);
return new Advancement(key, parent, display, AdvancementRewards.EMPTY, criteria, requirements, false); // TODO: 1.20: do we want to ever enable telemetry?
}

private static ResourceLocation asResourceLocation(NamespacedKey key) {
Expand Down
Expand Up @@ -40,7 +40,7 @@ public AnimationHelperImpl() {
register("IRON_GOLEM_ATTACK", entity -> {
if (entity instanceof IronGolem) {
Entity nmsEntity = ((CraftEntity) entity).getHandle();
nmsEntity.level.broadcastEntityEvent(nmsEntity, (byte) 4);
nmsEntity.level().broadcastEntityEvent(nmsEntity, (byte) 4);
}
});
}
Expand Down
Expand Up @@ -33,7 +33,6 @@
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.entity.SpawnerBlockEntity;
import net.minecraft.world.level.block.state.BlockBehaviour;
import net.minecraft.world.level.block.state.properties.NoteBlockInstrument;
import net.minecraft.world.level.chunk.ChunkAccess;
import net.minecraft.world.level.chunk.ChunkStatus;
import net.minecraft.world.level.material.PushReaction;
Expand Down Expand Up @@ -170,38 +169,33 @@ public org.bukkit.block.BlockState generateBlockState(Block block, Material mat)

public static final MethodHandle CRAFTBLOCKSTATE_CONSTRUCTOR = ReflectionHelper.getConstructor(CraftBlockState.class, Block.class);

public static final Field BLOCK_MATERIAL = ReflectionHelper.getFields(net.minecraft.world.level.block.state.BlockBehaviour.class).getFirstOfType(net.minecraft.world.level.material.Material.class);

public static final MethodHandle MATERIAL_PUSH_REACTION_SETTER = ReflectionHelper.getFinalSetterForFirstOfType(net.minecraft.world.level.material.Material.class, PushReaction.class);
public static final MethodHandle MATERIAL_PUSH_REACTION_SETTER = ReflectionHelper.getFinalSetterForFirstOfType(BlockBehaviour.BlockStateBase.class, PushReaction.class);

public static final MethodHandle BLOCK_STRENGTH_SETTER = ReflectionHelper.getFinalSetterForFirstOfType(net.minecraft.world.level.block.state.BlockBehaviour.BlockStateBase.class, float.class); // destroySpeed

public net.minecraft.world.level.block.Block getMaterialBlock(Material bukkitMaterial) {
public net.minecraft.world.level.block.state.BlockState getMaterialBlockState(Material bukkitMaterial) {
if (!bukkitMaterial.isBlock()) {
return null;
}
return ((CraftBlockData) bukkitMaterial.createBlockData()).getState().getBlock();
return ((CraftBlockData) bukkitMaterial.createBlockData()).getState();
}

public net.minecraft.world.level.material.Material getInternalMaterial(Material bukkitMaterial) {
try {
return (net.minecraft.world.level.material.Material) BLOCK_MATERIAL.get(getMaterialBlock(bukkitMaterial));
}
catch (Throwable ex) {
Debug.echoError(ex);
public net.minecraft.world.level.block.Block getMaterialBlock(Material bukkitMaterial) {
if (!bukkitMaterial.isBlock()) {
return null;
}
return ((CraftBlockData) bukkitMaterial.createBlockData()).getState().getBlock();
}

@Override
public String getPushReaction(Material mat) {
return getInternalMaterial(mat).getPushReaction().name();
return getMaterialBlockState(mat).getPistonPushReaction().name();
}

@Override
public void setPushReaction(Material mat, String reaction) {
try {
MATERIAL_PUSH_REACTION_SETTER.invoke(getInternalMaterial(mat), PushReaction.valueOf(reaction));
MATERIAL_PUSH_REACTION_SETTER.invoke(getMaterialBlockState(mat), PushReaction.valueOf(reaction));
}
catch (Throwable ex) {
Debug.echoError(ex);
Expand Down Expand Up @@ -259,10 +253,13 @@ public void doRandomTick(Location location) {

@Override
public Instrument getInstrumentFor(Material mat) {
return null; // TODO: 1.20
/*
net.minecraft.world.level.block.Block blockType = getMaterialBlock(mat);
Optional<NoteBlockInstrument> aboveInstrument = NoteBlockInstrument.byStateAbove(blockType.defaultBlockState());
NoteBlockInstrument nmsInstrument = aboveInstrument.orElse(NoteBlockInstrument.byStateBelow(blockType.defaultBlockState()));
return Instrument.values()[(nmsInstrument.ordinal())];
*/
}

@Override
Expand Down
Expand Up @@ -81,7 +81,7 @@ public void restoreServerThread(World world) {

@Override
public int[] getHeightMap(Chunk chunk) {
Heightmap map = ((CraftChunk) chunk).getHandle(ChunkStatus.HEIGHTMAPS).heightmaps.get(Heightmap.Types.MOTION_BLOCKING);
Heightmap map = ((CraftChunk) chunk).getHandle(ChunkStatus.FEATURES).heightmaps.get(Heightmap.Types.MOTION_BLOCKING);
int[] outputMap = new int[256];
for (int x = 0; x < 16; x++) {
for (int y = 0; y < 16; y++) {
Expand Down
Expand Up @@ -123,10 +123,10 @@ public double getDamageTo(LivingEntity attacker, Entity target) {
DamageSource source;
net.minecraft.world.entity.Entity nmsTarget = ((CraftEntity) target).getHandle();
if (attacker instanceof Player) {
source = nmsTarget.level.damageSources().playerAttack(((CraftPlayer) attacker).getHandle());
source = nmsTarget.level().damageSources().playerAttack(((CraftPlayer) attacker).getHandle());
}
else {
source = nmsTarget.level.damageSources().mobAttack(((CraftLivingEntity) attacker).getHandle());
source = nmsTarget.level().damageSources().mobAttack(((CraftLivingEntity) attacker).getHandle());
}
if (nmsTarget.isInvulnerableTo(source)) {
return 0;
Expand Down Expand Up @@ -350,7 +350,7 @@ public void run() {

@Override
public List<Player> getPlayersThatSee(Entity entity) {
ChunkMap tracker = ((ServerLevel) ((CraftEntity) entity).getHandle().level).getChunkSource().chunkMap;
ChunkMap tracker = ((ServerLevel) ((CraftEntity) entity).getHandle().level()).getChunkSource().chunkMap;
ChunkMap.TrackedEntity entityTracker = tracker.entityMap.get(entity.getEntityId());
ArrayList<Player> output = new ArrayList<>();
if (entityTracker == null) {
Expand All @@ -364,7 +364,7 @@ public List<Player> getPlayersThatSee(Entity entity) {

@Override
public void sendAllUpdatePackets(Entity entity) {
ChunkMap tracker = ((ServerLevel) ((CraftEntity) entity).getHandle().level).getChunkSource().chunkMap;
ChunkMap tracker = ((ServerLevel) ((CraftEntity) entity).getHandle().level()).getChunkSource().chunkMap;
ChunkMap.TrackedEntity entityTracker = tracker.entityMap.get(entity.getEntityId());
if (entityTracker == null) {
return;
Expand All @@ -391,7 +391,7 @@ public void sendHidePacket(Player pl, Entity entity) {
CraftPlayer craftPlayer = (CraftPlayer) pl;
ServerPlayer entityPlayer = craftPlayer.getHandle();
if (entityPlayer.connection != null && !craftPlayer.equals(entity)) {
ChunkMap tracker = ((ServerLevel) craftPlayer.getHandle().level).getChunkSource().chunkMap;
ChunkMap tracker = ((ServerLevel) craftPlayer.getHandle().level()).getChunkSource().chunkMap;
net.minecraft.world.entity.Entity other = ((CraftEntity) entity).getHandle();
ChunkMap.TrackedEntity entry = tracker.entityMap.get(other.getId());
if (entry != null) {
Expand All @@ -412,7 +412,7 @@ public void sendShowPacket(Player pl, Entity entity) {
CraftPlayer craftPlayer = (CraftPlayer) pl;
ServerPlayer entityPlayer = craftPlayer.getHandle();
if (entityPlayer.connection != null && !craftPlayer.equals(entity)) {
ChunkMap tracker = ((ServerLevel) craftPlayer.getHandle().level).getChunkSource().chunkMap;
ChunkMap tracker = ((ServerLevel) craftPlayer.getHandle().level()).getChunkSource().chunkMap;
net.minecraft.world.entity.Entity other = ((CraftEntity) entity).getHandle();
ChunkMap.TrackedEntity entry = tracker.entityMap.get(other.getId());
if (entry != null) {
Expand Down Expand Up @@ -611,14 +611,14 @@ public static DamageSources getReusableDamageSources() {
}

public static DamageSource getSourceFor(net.minecraft.world.entity.Entity nmsSource, EntityDamageEvent.DamageCause cause, net.minecraft.world.entity.Entity nmsSourceProvider) {
DamageSources sources = nmsSourceProvider == null ? getReusableDamageSources() : nmsSourceProvider.level.damageSources();
DamageSources sources = nmsSourceProvider == null ? getReusableDamageSources() : nmsSourceProvider.level().damageSources();
DamageSource src = sources.generic();
if (nmsSource != null) {
if (nmsSource instanceof net.minecraft.world.entity.player.Player) {
src = nmsSource.level.damageSources().playerAttack((net.minecraft.world.entity.player.Player) nmsSource);
src = nmsSource.level().damageSources().playerAttack((net.minecraft.world.entity.player.Player) nmsSource);
}
else if (nmsSource instanceof net.minecraft.world.entity.LivingEntity) {
src = nmsSource.level.damageSources().mobAttack((net.minecraft.world.entity.LivingEntity) nmsSource);
src = nmsSource.level().damageSources().mobAttack((net.minecraft.world.entity.LivingEntity) nmsSource);
}
}
if (cause == null) {
Expand Down Expand Up @@ -656,7 +656,7 @@ else if (nmsSource instanceof net.minecraft.world.entity.LivingEntity) {
case ENTITY_EXPLOSION:
return sources.explosion(nmsSource, null);
case VOID:
return sources.outOfWorld();
return sources.fellOutOfWorld();
case LIGHTNING:
return sources.lightningBolt();
case STARVATION:
Expand Down Expand Up @@ -810,7 +810,7 @@ public void setUUID(Entity entity, UUID id) {
net.minecraft.world.entity.Entity nmsEntity = ((CraftEntity) entity).getHandle();
nmsEntity.stopRiding();
nmsEntity.getPassengers().forEach(net.minecraft.world.entity.Entity::stopRiding);
Level level = nmsEntity.level;
Level level = nmsEntity.level();
DedicatedPlayerList playerList = ((CraftServer) Bukkit.getServer()).getHandle();
if (nmsEntity instanceof ServerPlayer) {
PLAYERLIST_REMOVE.invoke(playerList, nmsEntity);
Expand Down
Expand Up @@ -4,6 +4,7 @@
import com.denizenscript.denizen.nms.v1_20.ReflectionMappingsInfo;
import com.denizenscript.denizencore.utilities.debugging.Debug;
import com.denizenscript.denizencore.utilities.ReflectionHelper;
import com.google.common.collect.Maps;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.util.Mth;
Expand All @@ -14,7 +15,9 @@
import net.minecraft.world.item.enchantment.Enchantments;
import net.minecraft.world.level.storage.loot.BuiltInLootTables;
import net.minecraft.world.level.storage.loot.LootContext;
import net.minecraft.world.level.storage.loot.LootTables;
import net.minecraft.world.level.storage.loot.LootDataManager;
import net.minecraft.world.level.storage.loot.LootParams;
import net.minecraft.world.level.storage.loot.parameters.LootContextParam;
import net.minecraft.world.level.storage.loot.parameters.LootContextParamSets;
import net.minecraft.world.level.storage.loot.parameters.LootContextParams;
import net.minecraft.world.phys.Vec3;
Expand All @@ -29,6 +32,7 @@

import java.lang.reflect.Field;
import java.util.List;
import java.util.Map;

public class FishingHelperImpl implements FishingHelper {

Expand Down Expand Up @@ -76,13 +80,13 @@ else if (catchType == CatchType.FISH) {
}

public ItemStack getRandomReward(FishingHook hook, ResourceLocation key) {
ServerLevel worldServer = (ServerLevel) hook.level;
LootContext.Builder playerFishEvent2 = new LootContext.Builder(worldServer);
LootTables registry = ((ServerLevel) hook.level).getServer().getLootTables();
// registry.getLootTable(key).getLootContextParameterSet()
LootContext info = playerFishEvent2.withOptionalParameter(LootContextParams.ORIGIN, new Vec3(hook.getX(), hook.getY(), hook.getZ()))
.withOptionalParameter(LootContextParams.TOOL, new ItemStack(Items.FISHING_ROD)).create(LootContextParamSets.FISHING);
List<ItemStack> itemStacks = registry.get(key).getRandomItems(info);
ServerLevel worldServer = (ServerLevel) hook.level();
Map<LootContextParam<?>, Object> params = Maps.newIdentityHashMap();
params.put(LootContextParams.ORIGIN, new Vec3(hook.getX(), hook.getY(), hook.getZ()));
params.put(LootContextParams.TOOL, new ItemStack(Items.FISHING_ROD));
LootParams playerFishEvent2 = new LootParams(worldServer, params, Maps.newHashMap(), 0);
LootDataManager registry = worldServer.getServer().getLootData();
List<ItemStack> itemStacks = registry.getLootTable(key).getRandomItems(playerFishEvent2);
return itemStacks.get(worldServer.random.nextInt(itemStacks.size()));
}

Expand Down

0 comments on commit eba81d1

Please sign in to comment.