Skip to content

Commit

Permalink
Updated NMS usage to MC 1.12
Browse files Browse the repository at this point in the history
  • Loading branch information
c0wg0d committed Aug 4, 2018
1 parent ac69e31 commit 42584b2
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 54 deletions.
74 changes: 36 additions & 38 deletions src/main/java/me/libraryaddict/librarys/nms/FakeFurnaceImpl.java
Original file line number Diff line number Diff line change
@@ -1,23 +1,17 @@
package me.libraryaddict.librarys.nms;

import lombok.*;
import lombok.SneakyThrows;
import net.minecraft.server.v1_12_R1.*;
import org.bukkit.craftbukkit.v1_12_R1.entity.CraftPlayer;
import org.bukkit.craftbukkit.v1_12_R1.inventory.CraftItemStack;
import org.bukkit.entity.Player;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import net.minecraft.server.v1_8_R3.EntityHuman;
import net.minecraft.server.v1_8_R3.Item;
import net.minecraft.server.v1_8_R3.ItemStack;
import net.minecraft.server.v1_8_R3.RecipesFurnace;
import net.minecraft.server.v1_8_R3.TileEntityFurnace;

import org.bukkit.craftbukkit.v1_8_R3.entity.CraftPlayer;
import org.bukkit.craftbukkit.v1_8_R3.inventory.CraftItemStack;
import org.bukkit.entity.Player;

/* package */ class FakeFurnaceImpl extends TileEntityFurnace implements FakeFurnace {
private double burnSpeed;
private int lastID;
Expand All @@ -29,6 +23,8 @@
* displaying the progress I still have to use "cookTime"
*/
private double myCookTime;
private int burnTime;
private int cookTime;

@SneakyThrows(IllegalAccessException.class) // Shouldn't happen
public FakeFurnaceImpl() {
Expand Down Expand Up @@ -70,49 +66,49 @@ public void burn() {
if (!canBurn()) {
return;
}
ItemStack itemstack = getContents()[0] != null ? RecipesFurnace.getInstance().getResult(getContents()[0]) : null;
ItemStack itemstack = getContents().get(0) != null ? RecipesFurnace.getInstance().getResult(getContents().get(0)) : null;
// Nothing in there? Then put something there.
if (getContents()[2] == null) {
getContents()[2] = itemstack.cloneItemStack();
if (getContents().get(2) == null) {
getContents().set(2, itemstack.cloneItemStack());
}
// Burn ahead
else if (getContents()[2].doMaterialsMatch(itemstack)) {
getContents()[2].count += itemstack.count;
else if (getContents().get(2).doMaterialsMatch(itemstack)) {
getContents().get(2).setCount(getContents().get(2).getCount() + itemstack.getCount());
}
// Consume the ingredient item
Item craftingResult = getContents()[0].getItem().q();
Item craftingResult = getContents().get(0).getItem().q();
if (craftingResult != null) {
getContents()[0] = new ItemStack(craftingResult);
getContents().set(0, new ItemStack(craftingResult));
} else {
getContents()[0].count--;
getContents().get(0).setCount(getContents().get(0).getCount() - 1);
// Let 0 be null
if (getContents()[0].count <= 0) {
getContents()[0] = null;
if (getContents().get(0).getCount() <= 0) {
getContents().set(0, null);
}
}
}

private boolean canBurn() {
// No ingredient, no recipe
if (getContents()[0] == null) {
if (getContents().get(0) == null) {
return false;
}
ItemStack itemstack = RecipesFurnace.getInstance().getResult(getContents()[0]);
ItemStack itemstack = RecipesFurnace.getInstance().getResult(getContents().get(0));
// No recipe, no burning
if (itemstack == null) {
return false;
}
// Free space? Let's burn!
else if (getContents()[2] == null) {
else if (getContents().get(2) == null) {
return true;
}
// Materials don't match? Too bad.
else if (!getContents()[2].doMaterialsMatch(itemstack)) {
else if (!getContents().get(2).doMaterialsMatch(itemstack)) {
return false;
}
// As long as there is space, we can burn
else if ((getContents()[2].count + itemstack.count <= getMaxStackSize())
&& (getContents()[2].count + itemstack.count <= getContents()[2].getMaxStackSize())) {
else if ((getContents().get(2).getCount() + itemstack.getCount() <= getMaxStackSize())
&& (getContents().get(2).getCount() + itemstack.getCount() <= getContents().get(2).getMaxStackSize())) {
return true;
}
return false;
Expand Down Expand Up @@ -145,31 +141,31 @@ public final void c() {

@SneakyThrows(IllegalAccessException.class)
public void tick() {
int newID = getContents()[0] == null ? 0 : Item.getId(getContents()[0].getItem());
int newID = getContents().get(0) == null ? 0 : Item.getId(getContents().get(0).getItem());
// Has the item been changed?
if (newID != lastID) {
// Then reset the progress!
myCookTime = 0.0D;
lastID = newID;
// And, most important: change the melt speed
meltSpeed = getContents()[0] != null ? 1 : 0;
meltSpeed = getContents().get(0) != null ? 1 : 0;
}
// So, can we now finally burn?
if (canBurn() && !isBurning() && (getFuelTime(getContents()[1]) > 0)) {
if (canBurn() && !isBurning() && (getFuelTime(getContents().get(1)) > 0)) {
// I have no idea what "ticksForCurrentFuel" is good for, but it
// works fine like this
TICKS_FOR_CURRENT_FUEL_FIELD.setInt(this, burnTime = getFuelTime(getContents()[1]));
TICKS_FOR_CURRENT_FUEL_FIELD.setInt(this, burnTime = getFuelTime(getContents().get(1)));
// Before we remove the item: how fast does it burn?
burnSpeed = getBurnSpeed(getContents()[1]);
burnSpeed = getBurnSpeed(getContents().get(1));
// If it's a container item (lava bucket), we only consume its
// getContents() (not like evil Notch!)

// If it's not a container, consume it! Om nom nom nom!
{
getContents()[1].count--;
getContents().get(1).setCount(getContents().get(1).getCount() - 1);
// Let 0 be null
if (getContents()[1].count <= 0) {
getContents()[1] = null;
if (getContents().get(1).getCount() <= 0) {
getContents().set(1, null);
}
}
}
Expand Down Expand Up @@ -203,8 +199,10 @@ public void showTo(Player player) {

@Override
public List<org.bukkit.inventory.ItemStack> getItems() {
return Arrays.stream(getContents())
.map(CraftItemStack::asBukkitCopy)
.collect(Collectors.toList());
List<org.bukkit.inventory.ItemStack> items = new ArrayList<org.bukkit.inventory.ItemStack>();
for (ItemStack stack : getContents()) {
items.add(CraftItemStack.asBukkitCopy(stack));
}
return items;
}
}
28 changes: 12 additions & 16 deletions src/main/java/me/libraryaddict/librarys/nms/NMS.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,7 @@
import com.comphenix.protocol.ProtocolLibrary;
import com.comphenix.protocol.events.PacketContainer;

import net.minecraft.server.v1_8_R3.EnumParticle;
import net.minecraft.server.v1_8_R3.PacketPlayOutWorldEvent;

import org.bukkit.Location;
import org.bukkit.craftbukkit.v1_8_R3.CraftWorld;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
Expand Down Expand Up @@ -162,17 +158,17 @@ private static boolean hasIntersection(Vector3D p1, Vector3D p2, Vector3D min, V
}

public static void showPortalEffect(Location l) {
((CraftWorld) l.getWorld()).getHandle().a( // showParticle (http://wiki.vg/index.php?title=Protocol&oldid=7368#Particle)
EnumParticle.PORTAL,
l.getX(),
l.getY(),
l.getZ(),
// offsets (this is added to the X position after being multiplied by random.nextGaussian())
0,
0,
0, // Particle 'data'
1, // Particles to create
32 // Apparently this is how many particles an enderpearl creates,
);
// ((CraftWorld) l.getWorld()).getHandle().a( // showParticle (http://wiki.vg/index.php?title=Protocol&oldid=7368#Particle)
// EnumParticle.PORTAL,
// l.getX(),
// l.getY(),
// l.getZ(),
// // offsets (this is added to the X position after being multiplied by random.nextGaussian())
// 0,
// 0,
// 0, // Particle 'data'
// 1, // Particles to create
// 32 // Apparently this is how many particles an enderpearl creates,
// );
}
}

0 comments on commit 42584b2

Please sign in to comment.