Skip to content

Commit e5bfa06

Browse files
committed
Merge pull request tterrag1098#1 from impiaaa/master
Talking to your plants should help them grow
2 parents 3ad3f09 + 37f626e commit e5bfa06

File tree

6 files changed

+229
-34
lines changed

6 files changed

+229
-34
lines changed

build.gradle

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,6 @@ buildscript {
1313

1414
apply plugin: 'forge'
1515

16-
ext.configFile = file "build.properties"
17-
18-
configFile.withReader {
19-
// Load config. It shall from now be referenced as simply config or project.config
20-
def prop = new Properties()
21-
prop.load(it)
22-
project.ext.config = new ConfigSlurper().parse prop
23-
}
24-
2516
// Finds and sets version data
2617
task buildInfo {
2718
def cmd = "git rev-parse --short HEAD"
@@ -40,13 +31,13 @@ task buildInfo {
4031
}
4132
ext.artifact_version = 'NFG'
4233
if (System.getenv().ARTIFACT_VERSION == null) {
43-
artifact_version = "${config.mod_version}"
34+
artifact_version = "${mod_version}"
4435
}
4536
if (System.getenv().ARTIFACT_VERSION != null) {
4637
artifact_version = "${system.getenv().ARTIFACT_VERSION}"
4738
}
4839

49-
version = "MC${config.minecraft_version}-${artifact_version}-${buildInfo.buildNum}"
40+
version = "MC${minecraft_version}-${artifact_version}-${buildInfo.buildNum}"
5041
def actualVersion = "${artifact_version}-${buildInfo.buildNum}"
5142

5243
minecraft {
File renamed without changes.
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
package tterrag.treesimulator;
2+
3+
import java.io.ByteArrayOutputStream;
4+
import java.io.DataOutputStream;
5+
import java.io.IOException;
6+
import java.util.EnumSet;
7+
8+
import javax.sound.sampled.AudioFormat;
9+
import javax.sound.sampled.AudioSystem;
10+
import javax.sound.sampled.DataLine;
11+
import javax.sound.sampled.LineUnavailableException;
12+
import javax.sound.sampled.TargetDataLine;
13+
14+
import net.minecraft.client.Minecraft;
15+
import net.minecraft.network.packet.Packet250CustomPayload;
16+
import cpw.mods.fml.common.ITickHandler;
17+
import cpw.mods.fml.common.TickType;
18+
import cpw.mods.fml.common.network.PacketDispatcher;
19+
import cpw.mods.fml.relauncher.Side;
20+
import cpw.mods.fml.relauncher.SideOnly;
21+
22+
@SideOnly(Side.CLIENT)
23+
public class MicListener implements ITickHandler {
24+
private TargetDataLine line;
25+
private byte[] buffer;
26+
private int bufferOffset;
27+
public double bufferSizeMultiplier = 0.125;
28+
private boolean isRunning = false;
29+
30+
public void init()
31+
{
32+
AudioFormat format = new AudioFormat(8000.0F, 8, 1, true, true);
33+
DataLine.Info info = new DataLine.Info(TargetDataLine.class, format );
34+
if (!AudioSystem.isLineSupported(info)) {
35+
return;
36+
}
37+
// Obtain and open the line.
38+
try {
39+
line = (TargetDataLine) AudioSystem.getLine(info);
40+
line.open(format);
41+
} catch (LineUnavailableException ex) {
42+
return;
43+
}
44+
line.start();
45+
buffer = new byte[(int) (line.getBufferSize() * bufferSizeMultiplier)];
46+
bufferOffset = 0;
47+
isRunning = true;
48+
}
49+
50+
public void shutdown()
51+
{
52+
if (isRunning) {
53+
isRunning = false;
54+
line.close();
55+
}
56+
}
57+
58+
@Override
59+
public String getLabel() {
60+
return "treeSimulatorClientTickHandler";
61+
}
62+
63+
@Override
64+
public void tickEnd(EnumSet<TickType> type, Object... tickData) {
65+
}
66+
67+
@Override
68+
public void tickStart(EnumSet<TickType> type, Object... tickData) {
69+
if (isRunning && Minecraft.getMinecraft().inGameHasFocus)
70+
{
71+
int available = line.available();
72+
int remaining = buffer.length-bufferOffset;
73+
int bytesRead = line.read(buffer, bufferOffset, Math.min(remaining, available));
74+
bufferOffset += bytesRead;
75+
if (bytesRead <= available) {
76+
// buffer is full, analyze data
77+
bufferOffset = 0;
78+
int sum = 0;
79+
for (int i = 0; i < buffer.length; i++) {
80+
sum += buffer[i];
81+
}
82+
double avg = sum / buffer.length;
83+
double sumMeanSquare = 0.0;
84+
for(int i = 0; i < buffer.length; i++) {
85+
sumMeanSquare += Math.pow(buffer[i] - avg, 2.0);
86+
}
87+
88+
double averageMeanSquare = sumMeanSquare / buffer.length;
89+
double rms = Math.pow(averageMeanSquare,0.5d) + 0.5;
90+
91+
Packet250CustomPayload packet = new Packet250CustomPayload();
92+
packet.channel = TreeSimulator.CHANNEL;
93+
ByteArrayOutputStream byteStream = new ByteArrayOutputStream(12);
94+
DataOutputStream stream = new DataOutputStream(byteStream);
95+
try {
96+
stream.writeDouble(rms);
97+
stream.close();
98+
} catch (IOException e) {
99+
e.printStackTrace();
100+
return;
101+
}
102+
103+
packet.data = byteStream.toByteArray();
104+
packet.length = byteStream.size();
105+
PacketDispatcher.sendPacketToServer(packet);
106+
}
107+
}
108+
}
109+
110+
@Override
111+
public EnumSet<TickType> ticks() {
112+
return EnumSet.of(TickType.CLIENT);
113+
}
114+
}
Lines changed: 73 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,91 @@
11
package tterrag.treesimulator;
22

3+
import java.io.ByteArrayInputStream;
4+
import java.io.DataInputStream;
5+
import java.io.IOException;
6+
7+
import tterrag.treesimulator.TickHandlerTGS.PlayerState;
38
import net.minecraft.block.Block;
49
import net.minecraft.block.BlockSapling;
510
import net.minecraft.entity.player.EntityPlayer;
11+
import net.minecraft.entity.player.EntityPlayerMP;
612
import net.minecraft.network.INetworkManager;
713
import net.minecraft.network.packet.Packet250CustomPayload;
14+
import net.minecraftforge.common.MinecraftForge;
15+
import net.minecraftforge.event.entity.player.BonemealEvent;
816
import cpw.mods.fml.common.network.IPacketHandler;
917
import cpw.mods.fml.common.network.Player;
1018

1119
public class PacketHandlerTGS implements IPacketHandler{
12-
1320
@Override
14-
public void onPacketData(INetworkManager manager, Packet250CustomPayload packet, Player player) {
15-
16-
if (packet.data.length == 12)
17-
{
18-
byte[] bytes = packet.data;
19-
20-
int x = ((bytes[0] & 255) | ((bytes[1] & 255) << 8) | ((bytes[2] & 255) << 16) | (bytes[3] & 255) << 24);
21-
int y = ((bytes[4] & 255) | ((bytes[5] & 255) << 8) | ((bytes[6] & 255) << 16) | (bytes[7] & 255) << 24);
22-
int z = ((bytes[8] & 255) | ((bytes[9] & 255) << 8) | ((bytes[10] & 255) << 16) | (bytes[11] & 255) << 24);
21+
public void onPacketData(INetworkManager manager, Packet250CustomPayload packet, Player p) {
22+
if (p instanceof EntityPlayerMP) {
23+
// server side, "request growth" packet
24+
if (TreeSimulator.yellingWorks) {
25+
ByteArrayInputStream byteStream = new ByteArrayInputStream(packet.data);
26+
DataInputStream stream = new DataInputStream(byteStream);
27+
double rms;
28+
try {
29+
rms = stream.readDouble();
30+
} catch (IOException e) {
31+
e.printStackTrace();
32+
return;
33+
}
34+
if (rms > TreeSimulator.loudnessThreshold) {
35+
EntityPlayer player = (EntityPlayer) p;
36+
Player basePlayer = (Player) p;
37+
if (TreeSimulator.tickHandler.ticksSinceLastCheck >= 5)
38+
{
39+
int[] pos = TreeSimulator.tickHandler.getNearestSapling(player.worldObj, (int) player.posX, (int) player.posY, (int) player.posZ);
40+
41+
if (pos.length == 0)
42+
return;
43+
44+
TreeSimulator.tickHandler.movementCounter++;
45+
if (TreeSimulator.tickHandler.movementCounter > TreeSimulator.waitTime)
46+
{
47+
if (pos.length == 0)
48+
{
49+
TreeSimulator.tickHandler.movementCounter--;
50+
return;
51+
}
52+
53+
BonemealEvent event = new BonemealEvent(player, player.worldObj, player.worldObj.getBlockId(pos[0], pos[1], pos[2]), pos[0], pos[1], pos[2]);
54+
MinecraftForge.EVENT_BUS.post(event);
2355

24-
EntityPlayer entity = (EntityPlayer) player;
25-
Block block = Block.blocksList[entity.worldObj.getBlockId(x, y, z)];
26-
if (block instanceof BlockSapling)
56+
if ((double)player.worldObj.rand.nextFloat() < 0.45D)
57+
((BlockSapling)Block.blocksList[player.worldObj.getBlockId(pos[0], pos[1], pos[2])]).markOrGrowMarked(player.worldObj, pos[0], pos[1], pos[2], player.worldObj.rand);
58+
59+
if (TreeSimulator.showParticles)
60+
TreeSimulator.tickHandler.sendPacket(pos[0], pos[1], pos[2], player.worldObj, basePlayer);
61+
62+
TreeSimulator.tickHandler.movementCounter = 0;
63+
}
64+
}
65+
else
66+
{
67+
TreeSimulator.tickHandler.ticksSinceLastCheck++;
68+
}
69+
}
70+
}
71+
}
72+
else {
73+
// client side, particle effects packet
74+
if (packet.data.length == 12)
2775
{
28-
entity.worldObj.playAuxSFX(2005, x, y, z, 0);
76+
byte[] bytes = packet.data;
77+
78+
int x = ((bytes[0] & 255) | ((bytes[1] & 255) << 8) | ((bytes[2] & 255) << 16) | (bytes[3] & 255) << 24);
79+
int y = ((bytes[4] & 255) | ((bytes[5] & 255) << 8) | ((bytes[6] & 255) << 16) | (bytes[7] & 255) << 24);
80+
int z = ((bytes[8] & 255) | ((bytes[9] & 255) << 8) | ((bytes[10] & 255) << 16) | (bytes[11] & 255) << 24);
81+
82+
EntityPlayer entity = (EntityPlayer) p;
83+
Block block = Block.blocksList[entity.worldObj.getBlockId(x, y, z)];
84+
if (block instanceof BlockSapling)
85+
{
86+
entity.worldObj.playAuxSFX(2005, x, y, z, 0);
87+
}
2988
}
30-
System.out.println(String.format("Packet Recieved! block:%s x:%d y:%d z:%d", block == null ? "null" : block.toString(), x, y, z));
3189
}
3290
}
3391
}

src/main/java/tterrag/treesimulator/TickHandlerTGS.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,17 @@
1818

1919
public class TickHandlerTGS implements ITickHandler {
2020

21-
private double posX = 0, posZ = 0;
22-
private int movementCounter = 0;
23-
private int ticksSinceLastCheck = 0;
21+
public double posX = 0, posZ = 0;
22+
public int movementCounter = 0;
23+
public int ticksSinceLastCheck = 0;
2424

2525
public enum PlayerState {
2626
CROUCHED, STANDING;
2727

2828
public static PlayerState getState(boolean bool){ return bool ? CROUCHED : STANDING; };
2929
};
3030

31-
private PlayerState state = STANDING;
31+
public PlayerState state = STANDING;
3232

3333
@Override
3434
public void tickStart(EnumSet<TickType> type, Object... tickData)
@@ -83,7 +83,7 @@ public void tickStart(EnumSet<TickType> type, Object... tickData)
8383
}
8484
}
8585

86-
private void sendPacket(int x, int y, int z, World worldObj, Player player) {
86+
public void sendPacket(int x, int y, int z, World worldObj, Player player) {
8787

8888
Packet250CustomPayload packet = new Packet250CustomPayload();
8989

@@ -114,12 +114,12 @@ private void sendPacket(int x, int y, int z, World worldObj, Player player) {
114114
PacketDispatcher.sendPacketToPlayer(packet, player);
115115
}
116116

117-
private void updatePlayerPos(EntityPlayer player) {
117+
public void updatePlayerPos(EntityPlayer player) {
118118
posX = player.posX;
119119
posZ = player.posZ;
120120
}
121121

122-
private int[] getNearestSapling(World world, int xpos, int ypos, int zpos) {
122+
public int[] getNearestSapling(World world, int xpos, int ypos, int zpos) {
123123
for (int x = -5; x <= 5; x++)
124124
for (int y = -2; y <= 2; y++)
125125
for (int z = -5; z <= 5; z++) {

src/main/java/tterrag/treesimulator/TreeSimulator.java

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,26 @@
88
import cpw.mods.fml.common.Mod.Instance;
99
import cpw.mods.fml.common.event.FMLInitializationEvent;
1010
import cpw.mods.fml.common.event.FMLPreInitializationEvent;
11+
import cpw.mods.fml.common.event.FMLServerStartedEvent;
12+
import cpw.mods.fml.common.event.FMLServerStoppingEvent;
1113
import cpw.mods.fml.common.network.NetworkMod;
1214
import cpw.mods.fml.common.registry.TickRegistry;
1315
import cpw.mods.fml.relauncher.Side;
16+
import cpw.mods.fml.relauncher.SideOnly;
1417

1518
@Mod(modid = "treeGrowingSimulator", version = "0.0.3", name = "Tree Growing Simulator 2014")
1619
@NetworkMod(serverSideRequired=true, clientSideRequired=false, channels = {TreeSimulator.CHANNEL}, packetHandler = PacketHandlerTGS.class)
1720
public class TreeSimulator {
1821

1922
public static int waitTime;
2023
public static boolean showParticles;
24+
public static boolean yellingWorks;
25+
public static double loudnessThreshold;
2126
public static final String CHANNEL = "TGS2014";
27+
public static TickHandlerTGS tickHandler;
28+
29+
@SideOnly(Side.CLIENT)
30+
public MicListener micListener;
2231

2332
@Instance
2433
public static TreeSimulator instance;
@@ -32,7 +41,11 @@ public void preInit(FMLPreInitializationEvent event)
3241
@EventHandler
3342
public void init(FMLInitializationEvent event)
3443
{
35-
TickRegistry.registerTickHandler(new TickHandlerTGS(), Side.SERVER);
44+
tickHandler = new TickHandlerTGS();
45+
TickRegistry.registerTickHandler(tickHandler, Side.SERVER);
46+
if (event.getSide() == Side.CLIENT && yellingWorks) {
47+
micListener = new MicListener();
48+
}
3649
}
3750

3851
private void initConfig(File file)
@@ -43,7 +56,26 @@ private void initConfig(File file)
4356

4457
waitTime = config.get("Tweaks", "waitTime", 100, "The amount of ticks (times 5) you must be sprinting before bonemeal is applied").getInt();
4558
showParticles = config.get("Tweaks", "showParticles", true, "Show bonemeal particles when appropriate. Not sure why you would turn this off, but eh").getBoolean(true);
59+
yellingWorks = config.get("Tweaks", "yellingWorks", true, "Enable talking to speed growth").getBoolean(true);
60+
loudnessThreshold = config.get("Tweaks", "loudnessThreshold", 60.0, "What is considered to be the loudness of talking").getDouble(60.0);
4661

4762
config.save();
4863
}
64+
65+
@EventHandler
66+
public void gameStarted(FMLServerStartedEvent e) {
67+
if (e.getSide() == Side.CLIENT && yellingWorks)
68+
{
69+
micListener.init();
70+
TickRegistry.registerTickHandler(micListener, Side.CLIENT);
71+
}
72+
}
73+
74+
@EventHandler
75+
public void gameStopping(FMLServerStoppingEvent e) {
76+
if (e.getSide() == Side.CLIENT && yellingWorks)
77+
{
78+
micListener.shutdown();
79+
}
80+
}
4981
}

0 commit comments

Comments
 (0)